diff --git a/lcl/comctrls.pp b/lcl/comctrls.pp index 1adced376a..d0595a4f7b 100644 --- a/lcl/comctrls.pp +++ b/lcl/comctrls.pp @@ -1232,6 +1232,7 @@ type private FAllocBy: Integer; FAutoSort: Boolean; + FAutoWidthLastColumn: Boolean; FCanvas: TCanvas; FDefaultItemHeight: integer; FHotTrackStyles: TListHotTrackStyles; @@ -1289,7 +1290,9 @@ type function GetViewOrigin: TPoint; function GetVisibleRowCount: Integer; + procedure ResizeLastColumn; procedure SetAllocBy(const AValue: Integer); + procedure SetAutoWidthLastColumn(AValue: Boolean); procedure SetColumns(const AValue: TListColumns); procedure SetDefaultItemHeight(AValue: Integer); procedure SetDropTarget(const AValue: TListItem); @@ -1335,6 +1338,7 @@ type procedure DoInsert(AItem: TListItem); virtual; procedure DoItemChecked(AItem: TListItem); procedure DoSelectItem(AItem: TListItem; ASelected: Boolean); virtual; + procedure DoSetBounds(ALeft, ATop, AWidth, AHeight: integer); override; procedure InsertItem(Item : TListItem); procedure ImageChanged(Sender : TObject); procedure Loaded; override; @@ -1351,6 +1355,7 @@ type protected property AllocBy: Integer read FAllocBy write SetAllocBy default 0; property AutoSort: Boolean read FAutoSort write FAutoSort default True; // when we click header column sort automatically + property AutoWidthLastColumn: Boolean read FAutoWidthLastColumn write SetAutoWidthLastColumn default False; // resize last column to fit width of TListView property ColumnClick: Boolean index Ord(lvpColumnClick) read GetProperty write SetProperty default True; property Columns: TListColumns read FColumns write SetColumns; property DefaultItemHeight: integer read FDefaultItemHeight write SetDefaultItemHeight; @@ -1435,6 +1440,7 @@ type property AllocBy; property Anchors; property AutoSort; + property AutoWidthLastColumn: Boolean read FAutoWidthLastColumn write SetAutoWidthLastColumn default False; // resize last column to fit width of TListView property BorderSpacing; property BorderStyle; property BorderWidth; diff --git a/lcl/include/customlistview.inc b/lcl/include/customlistview.inc index 0ca9610f4e..25ba34d3b4 100644 --- a/lcl/include/customlistview.inc +++ b/lcl/include/customlistview.inc @@ -82,6 +82,7 @@ begin inherited Create(AOwner); ControlStyle := ControlStyle - [csCaptureMouse]; FAutoSort := True; + FAutoWidthLastColumn := False; FSortDirection := sdAscending; FIconOptions := TIconOptions.Create(Self); FColumns := TListColumns.Create(Self); @@ -452,6 +453,13 @@ begin FOnSelectItem(Self, AItem, ASelected); end; +procedure TCustomListView.DoSetBounds(ALeft, ATop, AWidth, AHeight: integer); +begin + inherited DoSetBounds(ALeft, ATop, AWidth, AHeight); + if AutoWidthLastColumn then + ResizeLastColumn; +end; + {------------------------------------------------------------------------------} { TCustomListView ItemDeleted } {------------------------------------------------------------------------------} @@ -1043,6 +1051,61 @@ begin TWSCustomListViewClass(WidgetSetClass).SetAllocBy(Self, AValue); end; +procedure TCustomListView.ResizeLastColumn; +var + i: Integer; + LastVisibleColumn: Integer; + Accu: Integer; + W: Integer; + NewWidth: Integer; +begin + if not (ViewStyle in [vsList, vsReport]) or (ColumnCount = 0) then + exit; + LastVisibleColumn := -1; + + // find last visible column + for i := ColumnCount - 1 downto 0 do + begin + if Column[i].Visible then + begin + LastVisibleColumn := i; + break; + end; + end; + + // calculate size and apply it only if it's > 0 + if LastVisibleColumn >= 0 then + begin + //TODO: gtk2 doesnt return correct ClientWidth. win32 and qt works ok. + W := ClientWidth - (BorderWidth * 2); + Accu := 0; + for i := 0 to LastVisibleColumn - 1 do + begin + if Column[i].Visible then + Accu := Accu + Column[i].Width; + end; + NewWidth := W - Accu; + if NewWidth > 0 then + begin + // now set AutoSize and MinWidth/MaxWidth to 0 + Column[LastVisibleColumn].AutoSize := False; + Column[LastVisibleColumn].MinWidth := 0; + Column[LastVisibleColumn].MaxWidth := 0; + Column[LastVisibleColumn].Width := NewWidth; + end; + end; +end; + +procedure TCustomListView.SetAutoWidthLastColumn(AValue: Boolean); +var + i: Integer; +begin + if FAutoWidthLastColumn=AValue then Exit; + FAutoWidthLastColumn:=AValue; + if FAutoWidthLastColumn then + ResizeLastColumn; +end; + procedure TCustomListView.SetDefaultItemHeight(AValue: Integer); begin if AValue <=0 then AValue := 20;