delphidabbler / codesnip

A code bank designed with Pascal in mind
https://delphidabbler.com/software/codesnip
Other
110 stars 33 forks source link

Reverse order of compilers in Configure Compilers dialogue box #51

Closed delphidabbler closed 10 months ago

delphidabbler commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 1 year ago

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

delphidabbler commented 1 year ago

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 commented 1 year ago

Implemented at merge commit a2835a34

Includes reversing list order in Find Compilers dialogue box