Recent

Author Topic: Cocoa ListView with OwnerData  (Read 515 times)

LazProgger

  • Jr. Member
  • **
  • Posts: 91
Cocoa ListView with OwnerData
« on: August 11, 2019, 05:11:51 pm »
I have tried to implement a ListView using OwnerData = true and an OnData procedure on Cocoa.

The data is only showing when setting Items.Count in the OnCreate procedure of the program, not when trying to add new items with setting Items.Count during run-time.

I have attached an example project and here is the code:

Code: Pascal  [Select]
  1.  
  2. unit Unit1;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
  10.   StdCtrls;
  11.  
  12. type
  13.  
  14.   { TForm1 }
  15.   TForm1 = class(TForm)
  16.     lv_def: TListView;
  17.     lv_virtual: TListView;
  18.     bt_add_item_lv_def: TButton;
  19.     bt_add_item_lv_virtual: TButton;
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure lv_virtualData(Sender: TObject; Item: TListItem);
  22.     procedure bt_add_item_lv_defClick(Sender: TObject);
  23.     procedure bt_add_item_lv_virtualClick(Sender: TObject);
  24.   private
  25.     { private declarations }
  26.   public
  27.     { public declarations }
  28.     procedure AddItemDef;
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. implementation
  35.  
  36. {$R *.lfm}
  37.  
  38. { TForm1 }
  39.  
  40. procedure TForm1.FormCreate(Sender: TObject);
  41. begin
  42.   // set items for default listview - works on Cocoa [items are displayed when starting the program]
  43.   self.AddItemDef;
  44.   self.AddItemDef;
  45.   self.AddItemDef;
  46.  
  47.   // set items for virtual listview - works on Cocoa [items are displayed when starting the program]
  48.   lv_virtual.Items.Count := 3;
  49. end;
  50.  
  51. procedure TForm1.bt_add_item_lv_defClick(Sender: TObject);
  52. begin
  53.   // add item to default listview - works on Cocoa [items are displayed after clicking the button]
  54.   self.AddItemDef;
  55. end;
  56.  
  57. procedure TForm1.bt_add_item_lv_virtualClick(Sender: TObject);
  58. begin
  59.   // add item to virtual listview - NOT WORKING on Cocoa [new items are not displayed after clicking the button]
  60.   lv_virtual.Items.Count := lv_virtual.Items.Count + 1;
  61.   self.Caption := IntToStr(lv_virtual.Items.Count);  // shows the correct item count
  62.   //lv_virtual.Invalidate;  - no effect
  63.   //lv_virtual.Refresh;     - no effect
  64.   //lv_virtual.Repaint;     - no effect
  65.   //lv_virtual.Update;      - no effect
  66. end;
  67.  
  68. procedure TForm1.lv_virtualData(Sender: TObject; Item: TListItem);
  69. begin
  70.   // fetch virtual listview data
  71.   Item.Caption := 'Cap'+inttostr(item.Index);
  72.   Item.SubItems.Add('Sub1');
  73.   Item.SubItems.Add('Sub2');
  74. end;
  75.  
  76. procedure TForm1.AddItemDef;
  77. var
  78.   AItem: TListItem;
  79. begin
  80.   // add item to default listview - works on Cocoa
  81.   AItem := lv_def.Items.Add;
  82.   AItem.Caption := 'Cap'+inttostr(lv_def.Items.Count);
  83.   AItem.SubItems.Add('Sub1');
  84.   AItem.SubItems.Add('Sub2');
  85. end;
  86.  
  87. end.  
  88.  
  89.  

At startup, the ListView has 3 columns, at run-time, you can add new items with the Add-button.

On Windows, it is working in both situations; on Cocoa the Add-button procedure is not working. I think there is some procedure for the refresh missing. I have tried using .Invalidate, .Refresh, .Repaint and .Update but the items are not displayed.

Does someone have an idea?
« Last Edit: August 11, 2019, 10:33:11 pm by LazProgger »

Wallaby

  • New Member
  • *
  • Posts: 22
Re: Cocoa ListView with OwnerData
« Reply #1 on: August 12, 2019, 05:14:46 am »
I have experienced the same problem and instead decided to use Virtual Tree View.

However TListView should be fixed (@dmitri - please!)

LazProgger

  • Jr. Member
  • **
  • Posts: 91
Re: Cocoa ListView with OwnerData
« Reply #2 on: August 12, 2019, 02:40:33 pm »
I have added a bug report:

https://bugs.freepascal.org/view.php?id=35957

Because it's working in initialization, I think to fix it, you only have to add an refresh or a procedure call at some point, probably one line of code. However, I don't know where, otherwise I would to that.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2268
    • havefunsoft.com
Re: Cocoa ListView with OwnerData
« Reply #3 on: August 12, 2019, 04:53:21 pm »
I have experienced the same problem and instead decided to use Virtual Tree View.
hmm... were there any other issues with TListView?

The data is only showing when setting Items.Count in the OnCreate procedure of the program, not when trying to add new items with setting Items.Count during run-time.
try the latest trunk
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

LazProgger

  • Jr. Member
  • **
  • Posts: 91
Re: Cocoa ListView with OwnerData
« Reply #4 on: August 12, 2019, 06:12:39 pm »
Thank you very much for fixing! My example project is working now.

hmm... were there any other issues with TListView?

At the moment, I have not found any other issue, but I will come back if I see one.

@Wallaby: Have you had other issues?
« Last Edit: August 12, 2019, 06:14:12 pm by LazProgger »

LazProgger

  • Jr. Member
  • **
  • Posts: 91
Re: Cocoa ListView with OwnerData
« Reply #5 on: August 14, 2019, 09:26:24 pm »
hmm... were there any other issues with TListView?

I have found another issue:

If you have OwnerData = true and Checkboxes = true and if you try to set the checkbox state in the OnData procedure like that

Code: Pascal  [Select]
  1. procedure TForm1.lv_virtualData(Sender: TObject; Item: TListItem);
  2. begin
  3.   Item.Caption := 'Cap'+inttostr(item.Index);
  4.   Item.SubItems.Add('Sub1');
  5.   Item.SubItems.Add('Sub2');
  6.   Item.Checked := true;
  7. end;

then the checkbox remains unchecked.

Apart from that, the Checkboxes are display correctly and you can change their state with the mouse properly.

Another problem is with the OnItemChecked event. It seems to fire only when the checkboxes are unchecked (only when the checkbox is checked and you uncheck it, not if the checkbox is not checked and you check it). On Windows, the event fires in both situations on every checkbox state change.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2268
    • havefunsoft.com
Re: Cocoa ListView with OwnerData
« Reply #6 on: August 14, 2019, 10:46:53 pm »
If you have OwnerData = true and Checkboxes = true and if you try to set the checkbox state in the OnData procedure like that
this one should be addressed in r61695

and that might also fix the issue with OnItemChecked.
« Last Edit: August 14, 2019, 10:49:48 pm by skalogryz »
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

LazProgger

  • Jr. Member
  • **
  • Posts: 91
Re: Cocoa ListView with OwnerData
« Reply #7 on: August 15, 2019, 03:57:15 am »
Great! You are fixing the issues faster than we can test!

By the way: Is there any way to see the changes in trunk from revision to revision? Something like how it its shown on sourcefourge where you can see which files were changed when? Up to now, I only know the site https://svn.freepascal.org/svn/lazarus/ where you can only see the files in its last state.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2268
    • havefunsoft.com
Re: Cocoa ListView with OwnerData
« Reply #8 on: August 15, 2019, 04:44:21 am »
By the way: Is there any way to see the changes in trunk from revision to revision?
if you don't want to use svn client, then you could use the ViewVC utility that's used on the repository
https://svn.freepascal.org/cgi-bin/viewvc.cgi/

This is how this particular fix looks like:
https://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/lcl/interfaces/cocoa/cocoawscomctrls.pas?root=lazarus&r1=61695&r2=61694&pathrev=61695
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

LazProgger

  • Jr. Member
  • **
  • Posts: 91
Re: Cocoa ListView with OwnerData
« Reply #9 on: August 15, 2019, 02:08:57 pm »
Thanks! That is what I was searching for.