View Issue Details

IDProjectCategoryView StatusLast Update
0021745LazarusLCLpublic2012-04-24 14:40
ReporterDavid Jenkins Assigned ToZeljan Rikalo  
PrioritynormalSeverityfeatureReproducibilityN/A
Status closedResolutionfixed 
Product Version0.9.30.5 (SVN) 
Summary0021745: AutoSizing for Carbon ListView / DataBrowser Columns
DescriptionAdd ListView and DataBrowser column autosizing base on available space and number of AutoSize'd columns.

Suggest additions are adding ColumnSetAutoSize to carbonWSCusomtListView , SetAutoSize to CarbonListColumn, AutoSizeColumns to CarbonDataBrowser, and set up calls to AutoSizeColumns

Patches against rev 36681 attached
TagsNo tags attached.
Fixed in Revision36961
LazTarget-
WidgetsetCarbon
Attached Files

Activities

2012-04-13 18:46

 

carbonlistviews.pp.patch (2,989 bytes)   
--- /Users/djenkins/laz-changes/13828/carbonlistviews.pp	2012-04-09 16:09:52.000000000 
+++ /Users/djenkins/laz-changes/13828/carbonlistviews.pp.ss	2012-04-09 16:14:31.000000000 
@@ -50,6 +50,7 @@
     FWidth: Integer;
     FIndex: Integer; // index of TListColumn
     fTextWithIcon: Boolean;
+    FAutoSize: Boolean;
 
     procedure UpdateHeader;
     function GetHeaderWidth: UInt16;
@@ -72,13 +73,14 @@
   public
     function GetWidth: Integer;
     procedure SetAlignment(AAlignment: TAlignment);
-    // TODO: SetAutoSize
+    procedure SetAutoSize(AValue: Boolean);
     procedure SetCaption(const ACaption: String);
     procedure SetImageIndex(AImageIndex: Integer);
     procedure SetMinWidth(AMinWidth: Integer);
     procedure SetMaxWidth(AMaxWidth: Integer);
     procedure SetVisible(AVisible: Boolean);
-    procedure SetWidth(AWidth: Integer; AAutoSize: Boolean);
+    procedure SetWidth(AWidth: Integer);
+    property AutoSize: Boolean read FAutoSize write SetAutoSize;
     property TextWithIcon: Boolean read fTextWithIcon write fTextWithIcon;
   end;
 
@@ -186,6 +188,7 @@
     procedure MoveColumn(AOldIndex, ANewIndex: Integer; const AColumn: TListColumn);
     procedure UpdateColumnIndex;
     procedure UpdateColumnView; virtual;
+    procedure AutoSizeColumns;
 
     procedure ClearItems;
     procedure DeleteItem(AIndex: Integer);
@@ -543,6 +546,11 @@
   if FVisible then UpdateHeader;
 end;
 
+procedure TCarbonListColumn.SetAutoSize(AValue: Boolean);
+begin
+  FAutoSize := AValue;
+end;
+
 procedure TCarbonListColumn.SetCaption(const ACaption: String);
 begin
   FreeCFString(FDesc.headerBtnDesc.titleString);
@@ -583,21 +591,11 @@
   else Remove;
 end;
 
-procedure TCarbonListColumn.SetWidth(AWidth: Integer; AAutoSize: Boolean);
+procedure TCarbonListColumn.SetWidth(AWidth: Integer);
 var
   lBmp: TBitmap;
 begin
-  // Implements Column Autosizing
-  if AAutoSize then
-  begin
-    lBmp := TBitmap.Create;
-    // The standard Mac listview font is quite bigger then the standard TCanvas font
-    // plus, we also need an extra spacing
-    FWidth := lBmp.Canvas.TextWidth(FListColumn.Caption) * 2;
-    lBmp.Free;
-  end
-  else
-    FWidth := AWidth;
+  FWidth := AWidth;
 
   if FVisible then SetHeaderWidth(FWidth);
 end;
@@ -1496,6 +1494,29 @@
 
 end;
 
+procedure TCarbonDataBrowser.AutoSizeColumns;
+var cnt, aCnt, tCnt: Integer;
+    sWidth, aWidth: Integer;
+    cRect: TRect;
+begin
+  GetClientRect(cRect);
+  sWidth := 0;
+  aCnt := 0;
+  tCnt := 0;
+  while GetColumn(tCnt) <> nil do begin
+      if GetColumn(tCnt).AutoSize then Inc(aCnt)
+      else Inc(sWidth, GetColumn(tCnt).GetWidth);
+      Inc(tCnt);
+  end;
+
+  if aCnt > 0 then begin
+  	 aWidth := ((cRect.Right - cRect.Left) - sWidth) div aCnt;
+  	 for cnt := 0 to tCnt - 1 do
+       if GetColumn(cnt).AutoSize then
+         GetColumn(cnt).SetWidth(aWidth);
+  end;
+end;
+
 procedure TCarbonDataBrowser.DeleteItem(AIndex: Integer);
 var
   Item  : DataBrowserItemID;
carbonlistviews.pp.patch (2,989 bytes)   

2012-04-13 18:46

 

carbonwscomctrls.pp.patch (2,477 bytes)   
--- /Users/djenkins/laz-changes/13828/carbonwscomctrls.pp	2012-04-09 16:18:39.000000000 
+++ /Users/djenkins/laz-changes/13828/carbonwscomctrls.pp.ss	2012-04-09 16:19:01.000000000 
@@ -103,7 +103,7 @@
     class procedure ColumnInsert(const ALV: TCustomListView; const AIndex: Integer; const AColumn: TListColumn); override;
     class procedure ColumnMove(const ALV: TCustomListView; const AOldIndex, ANewIndex: Integer; const AColumn: TListColumn); override;
     class procedure ColumnSetAlignment(const ALV: TCustomListView; const AIndex: Integer; const AColumn: TListColumn; const AAlignment: TAlignment); override;
-    //class procedure ColumnSetAutoSize(const ALV: TCustomListView; const AIndex: Integer; const AColumn: TListColumn; const AAutoSize: Boolean); override;
+    class procedure ColumnSetAutoSize(const ALV: TCustomListView; const AIndex: Integer; const AColumn: TListColumn; const AAutoSize: Boolean); override;
     class procedure ColumnSetCaption(const ALV: TCustomListView; const AIndex: Integer; const AColumn: TListColumn; const ACaption: String); override;
     class procedure ColumnSetImage(const ALV: TCustomListView; const AIndex: Integer; const AColumn: TListColumn; const AImageIndex: Integer); override;
     class procedure ColumnSetMaxWidth(const ALV: TCustomListView; const AIndex: Integer; const AColumn: TListColumn; const AMaxWidth: Integer); override;
@@ -519,6 +519,15 @@
   TCarbonListView(ALV.Handle).GetColumn(AIndex).SetAlignment(AAlignment);
 end;
 
+class procedure TCarbonWSCustomListView.ColumnSetAutoSize(const ALV: TCustomListView;
+  const AIndex: Integer; const AColumn: TListColumn; const AAutoSize: Boolean);
+begin
+  if not CheckHandle(ALV, Self, 'ColumnSetAutoSize') then Exit;
+
+  TCarbonListView(ALV.Handle).GetColumn(AIndex).SetAutoSize(AAutoSize);
+  TCarbonListView(ALV.Handle).AutoSizeColumns;
+end;
+
 class procedure TCarbonWSCustomListView.ColumnSetCaption(const ALV: TCustomListView;
   const AIndex: Integer; const AColumn: TListColumn; const ACaption: String);
 begin
@@ -559,7 +568,8 @@
   if not CheckHandle(ALV, Self, 'ColumnSetWidth') then Exit;
 
   Column := TCarbonListView(ALV.Handle).GetColumn(AIndex);
-  if Column <> nil then Column.SetWidth(AWidth, AColumn.AutoSize); // Avoids crash
+  if Column <> nil then Column.SetWidth(AWidth); // Avoids crash
+  TCarbonListView(ALV.Handle).AutoSizeColumns;
 end;
 
 class procedure TCarbonWSCustomListView.ColumnSetVisible(const ALV: TCustomListView;
carbonwscomctrls.pp.patch (2,477 bytes)   

2012-04-19 21:03

 

svn_patch-21745 (5,585 bytes)   
Index: lcl/interfaces/carbon/carbonlistviews.pp
===================================================================
--- lcl/interfaces/carbon/carbonlistviews.pp	(revision 36681)
+++ lcl/interfaces/carbon/carbonlistviews.pp	(working copy)
@@ -50,6 +50,7 @@
     FWidth: Integer;
     FIndex: Integer; // index of TListColumn
     fTextWithIcon: Boolean;
+    FAutoSize: Boolean;
 
     procedure UpdateHeader;
     function GetHeaderWidth: UInt16;
@@ -72,13 +73,14 @@
   public
     function GetWidth: Integer;
     procedure SetAlignment(AAlignment: TAlignment);
-    // TODO: SetAutoSize
+    procedure SetAutoSize(AValue: Boolean);
     procedure SetCaption(const ACaption: String);
     procedure SetImageIndex(AImageIndex: Integer);
     procedure SetMinWidth(AMinWidth: Integer);
     procedure SetMaxWidth(AMaxWidth: Integer);
     procedure SetVisible(AVisible: Boolean);
-    procedure SetWidth(AWidth: Integer; AAutoSize: Boolean);
+    procedure SetWidth(AWidth: Integer);
+    property AutoSize: Boolean read FAutoSize write SetAutoSize;
     property TextWithIcon: Boolean read fTextWithIcon write fTextWithIcon;
   end;
 
@@ -186,6 +188,7 @@
     procedure MoveColumn(AOldIndex, ANewIndex: Integer; const AColumn: TListColumn);
     procedure UpdateColumnIndex;
     procedure UpdateColumnView; virtual;
+    procedure AutoSizeColumns;
 
     procedure ClearItems;
     procedure DeleteItem(AIndex: Integer);
@@ -543,6 +546,11 @@
   if FVisible then UpdateHeader;
 end;
 
+procedure TCarbonListColumn.SetAutoSize(AValue: Boolean);
+begin
+  FAutoSize := AValue;
+end;
+
 procedure TCarbonListColumn.SetCaption(const ACaption: String);
 begin
   FreeCFString(FDesc.headerBtnDesc.titleString);
@@ -583,21 +591,11 @@
   else Remove;
 end;
 
-procedure TCarbonListColumn.SetWidth(AWidth: Integer; AAutoSize: Boolean);
+procedure TCarbonListColumn.SetWidth(AWidth: Integer);
 var
   lBmp: TBitmap;
 begin
-  // Implements Column Autosizing
-  if AAutoSize then
-  begin
-    lBmp := TBitmap.Create;
-    // The standard Mac listview font is quite bigger then the standard TCanvas font
-    // plus, we also need an extra spacing
-    FWidth := lBmp.Canvas.TextWidth(FListColumn.Caption) * 2;
-    lBmp.Free;
-  end
-  else
-    FWidth := AWidth;
+  FWidth := AWidth;
 
   if FVisible then SetHeaderWidth(FWidth);
 end;
@@ -1496,6 +1494,29 @@
 
 end;
 
+procedure TCarbonDataBrowser.AutoSizeColumns;
+var cnt, aCnt, tCnt: Integer;
+    sWidth, aWidth: Integer;
+    cRect: TRect;
+begin
+  GetClientRect(cRect);
+  sWidth := 0;
+  aCnt := 0;
+  tCnt := 0;
+  while GetColumn(tCnt) <> nil do begin
+      if GetColumn(tCnt).AutoSize then Inc(aCnt)
+      else Inc(sWidth, GetColumn(tCnt).GetWidth);
+      Inc(tCnt);
+  end;
+
+  if aCnt > 0 then begin
+  	 aWidth := ((cRect.Right - cRect.Left) - sWidth) div aCnt;
+  	 for cnt := 0 to tCnt - 1 do
+       if GetColumn(cnt).AutoSize then
+         GetColumn(cnt).SetWidth(aWidth);
+  end;
+end;
+
 procedure TCarbonDataBrowser.DeleteItem(AIndex: Integer);
 var
   Item  : DataBrowserItemID;
Index: lcl/interfaces/carbon/carbonwscomctrls.pp
===================================================================
--- lcl/interfaces/carbon/carbonwscomctrls.pp	(revision 36681)
+++ lcl/interfaces/carbon/carbonwscomctrls.pp	(working copy)
@@ -103,7 +103,7 @@
     class procedure ColumnInsert(const ALV: TCustomListView; const AIndex: Integer; const AColumn: TListColumn); override;
     class procedure ColumnMove(const ALV: TCustomListView; const AOldIndex, ANewIndex: Integer; const AColumn: TListColumn); override;
     class procedure ColumnSetAlignment(const ALV: TCustomListView; const AIndex: Integer; const AColumn: TListColumn; const AAlignment: TAlignment); override;
-    //class procedure ColumnSetAutoSize(const ALV: TCustomListView; const AIndex: Integer; const AColumn: TListColumn; const AAutoSize: Boolean); override;
+    class procedure ColumnSetAutoSize(const ALV: TCustomListView; const AIndex: Integer; const AColumn: TListColumn; const AAutoSize: Boolean); override;
     class procedure ColumnSetCaption(const ALV: TCustomListView; const AIndex: Integer; const AColumn: TListColumn; const ACaption: String); override;
     class procedure ColumnSetImage(const ALV: TCustomListView; const AIndex: Integer; const AColumn: TListColumn; const AImageIndex: Integer); override;
     class procedure ColumnSetMaxWidth(const ALV: TCustomListView; const AIndex: Integer; const AColumn: TListColumn; const AMaxWidth: Integer); override;
@@ -519,6 +519,15 @@
   TCarbonListView(ALV.Handle).GetColumn(AIndex).SetAlignment(AAlignment);
 end;
 
+class procedure TCarbonWSCustomListView.ColumnSetAutoSize(const ALV: TCustomListView;
+  const AIndex: Integer; const AColumn: TListColumn; const AAutoSize: Boolean);
+begin
+  if not CheckHandle(ALV, Self, 'ColumnSetAutoSize') then Exit;
+
+  TCarbonListView(ALV.Handle).GetColumn(AIndex).SetAutoSize(AAutoSize);
+  TCarbonListView(ALV.Handle).AutoSizeColumns;
+end;
+
 class procedure TCarbonWSCustomListView.ColumnSetCaption(const ALV: TCustomListView;
   const AIndex: Integer; const AColumn: TListColumn; const ACaption: String);
 begin
@@ -559,7 +568,8 @@
   if not CheckHandle(ALV, Self, 'ColumnSetWidth') then Exit;
 
   Column := TCarbonListView(ALV.Handle).GetColumn(AIndex);
-  if Column <> nil then Column.SetWidth(AWidth, AColumn.AutoSize); // Avoids crash
+  if Column <> nil then Column.SetWidth(AWidth); // Avoids crash
+  TCarbonListView(ALV.Handle).AutoSizeColumns;
 end;
 
 class procedure TCarbonWSCustomListView.ColumnSetVisible(const ALV: TCustomListView;
svn_patch-21745 (5,585 bytes)   

Zeljan Rikalo

2012-04-21 17:31

developer   ~0058847

Please test and close if ok

Issue History

Date Modified Username Field Change
2012-04-13 18:46 David Jenkins New Issue
2012-04-13 18:46 David Jenkins File Added: carbonlistviews.pp.patch
2012-04-13 18:46 David Jenkins Widgetset => Carbon
2012-04-13 18:46 David Jenkins File Added: carbonwscomctrls.pp.patch
2012-04-19 21:03 David Jenkins File Added: svn_patch-21745
2012-04-21 17:31 Zeljan Rikalo Fixed in Revision => 36961
2012-04-21 17:31 Zeljan Rikalo LazTarget => -
2012-04-21 17:31 Zeljan Rikalo Status new => resolved
2012-04-21 17:31 Zeljan Rikalo Resolution open => fixed
2012-04-21 17:31 Zeljan Rikalo Assigned To => Zeljan Rikalo
2012-04-21 17:31 Zeljan Rikalo Note Added: 0058847
2012-04-24 14:40 David Jenkins Status resolved => closed