codesnip icon indicating copy to clipboard operation
codesnip copied to clipboard

Reverse order of compilers Configure Compilers dialogue box

Open delphidabbler opened this issue 2 years ago • 2 comments

The list of compilers in the Configure Compilers dialogue box starts with Delphi 2 and you have to scroll to get to any of the more recent compilers. Reversing the order in the list would make it easier to edit the later compilers.

delphidabbler avatar May 15 '22 11:05 delphidabbler

The current code maps the compiler ID to the compiler list box index:

https://github.com/delphidabbler/codesnip/blob/19f0ca5234f488d75625b2ecf69b3213bd49cc41/Src/FmCompilersDlg.UCompilerListMgr.pas#L106

This relationship is also relied upon in the custom draw handler:

https://github.com/delphidabbler/codesnip/blob/19f0ca5234f488d75625b2ecf69b3213bd49cc41/Src/FmCompilersDlg.UCompilerListMgr.pas#L142

We're going to need to either (a) create a parallel array to the list items mapping item indices to compiler IDs or (b) to create custom list items that store the related compiler ID.

Not good code, but there it is.

delphidabbler avatar Jul 07 '22 00:07 delphidabbler

The parallel array method may be the easiest to implement. Some ideas:

Declare fields

fMapIdxToComp: TArray<TCompilerID>;
fMapCompToIdx: array[TCompilerID] of Integer;

In the Initialise method:

procedure TCompilerListMgr.Initialise;
var
  CompID: TCompilerID;  // loops thru supported compilers
  Idx: Integer;
begin
  inherited;
  // Add empty list items - one per supported compiler. Note we don't need item
  // text since we handle drawing of list items ourselves and get details from
  // compiler objects.
  SetLength(fMapIdxToComp, Length (fMapCompToIdx);
  Idx := 0;
  for CompID := High(TCompilerID) downto LowTCompilerID) do
  begin
    fLB.Items.Add('');
    fMapIdxToComp[Idx] := CompID;
    fMapCompToIdx[CompID] := Idx;
    Inc(Idx);
  end;
  // Select first compiler in list and trigger selection event for it
  fLB.ItemIndex := 0;
  DoSelect;
end;

In the GetSelected method:

function TCompilerListMgr.GetSelected: ICompiler;
begin
  Result := fCompilers[fMapIdxToComp[fLB.ItemIndex]];
end;

And in list box custom draw method:

procedure TCompilerListMgr.LBDrawItemHandler(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
begin
  ...
  // Compiler object associated with list item
  Compiler := fCompilers[fMapIdxToComp[fLB.ItemIndex]];
  ...
end;

And the Refresh method:

procedure TCompilerListMgr.Refresh(Compiler: ICompiler);
var
  InvalidRect: TRectEx;
begin
  InvalidRect := fLB.ItemRect(fMapCompIDToIdx[Compiler.GetID]);
  InvalidateRect(fLB.Handle, @InvalidRect, False);
end;

Not tested any of this.

delphidabbler avatar Jul 07 '22 01:07 delphidabbler

💡 Order of compilers in Find Compilers dialogue box could also benefit from being revered.

delphidabbler avatar Dec 12 '22 04:12 delphidabbler

The code in https://github.com/delphidabbler/codesnip/issues/51#issuecomment-1176928799 sort of worked, after a few tweaks, but it left the order of compilers exactly as it was before. However it was fairly easy to reverse the order of compilers in the lookup arrays.

delphidabbler avatar Dec 18 '22 00:12 delphidabbler

Implemented at merge commit a2835a34

Includes reversing list order in Find Compilers dialogue box

delphidabbler avatar Jul 14 '23 18:07 delphidabbler