Recent

Author Topic: TListView: onCustomDraw item, canvas.brush.color not working  (Read 17962 times)

Hansaplast

  • Hero Member
  • *****
  • Posts: 534
  • Tweaking4All.com
    • Tweaking4All
TListView: onCustomDraw item, canvas.brush.color not working
« on: January 14, 2011, 03:56:32 am »
The onCustomDrawItem does not seem to work properly under the Carbon widgetset.
I think this post refers to the same problem.

I noticed a few resolved and close bug reports for other widget sets, and some work around tips for those widgetsets as well, but I simply cannot get it to work under Carbon.

I'm trying to accomplish alternating row colors in a TListView (see also this article).

Does anyone know how to get this done?
Or should I post a bug?

cpalx

  • Hero Member
  • *****
  • Posts: 606
Re: TListView: onCustomDraw item, canvas.brush.color not working
« Reply #1 on: January 14, 2011, 04:01:19 am »
Post a bug. Listview is not a fully implmented component

Hansaplast

  • Hero Member
  • *****
  • Posts: 534
  • Tweaking4All.com
    • Tweaking4All
Re: TListView: onCustomDraw item, canvas.brush.color not working
« Reply #2 on: January 14, 2011, 04:13:45 am »
I just noticed that the onCustomDrawItem or onAdvancedCustomDraw simply never fire ...?

Do I need to set a property to have this fire?

Phil

  • Hero Member
  • *****
  • Posts: 2750
Re: TListView: onCustomDraw item, canvas.brush.color not working
« Reply #3 on: January 14, 2011, 04:16:45 am »
You are setting a property when you double-click or click button in Object Inspector event.

If you just typed in the event handler, then select it for the appropriate event in Object Inspector.

Thanks.

-Phil

Hansaplast

  • Hero Member
  • *****
  • Posts: 534
  • Tweaking4All.com
    • Tweaking4All
Re: TListView: onCustomDraw item, canvas.brush.color not working
« Reply #4 on: January 14, 2011, 04:49:11 am »
I double clicked the event to create the event handler.
That procedure does not get fired ever ...!?

I have also been looking for such a property (like Style for a TListBox -> lbOwnerDrawFixed), but didn't find anything like that for a TListView  :o

Phil

  • Hero Member
  • *****
  • Posts: 2750
Re: TListView: onCustomDraw item, canvas.brush.color not working
« Reply #5 on: January 14, 2011, 04:59:17 am »
What version of Lazarus are you using?

Here's the relevant code for that with the current 0.9.29 (in customlistview.inc):

function TCustomListView.CustomDrawItem(AItem: TListItem; AState: TCustomDrawState; AStage: TCustomDrawStage): Boolean;
begin
  Result := True;
  if Assigned(FOnCustomDrawItem) and (AStage = cdPrePaint)
  then FOnCustomDrawItem(Self, AItem, AState, Result);
 
  if Assigned(FOnAdvancedCustomDrawItem)
  then FOnAdvancedCustomDrawItem(Self, AItem, AState, AStage, Result);
end;

So the OnCustomDrawItem event handler gets called during the "prepaint" stage.

The OnAdvancedCustomDrawItem event handler gets called during each stage, it looks like. You can decide in your handler which to respond to. Defined as (in comctrls.pp):

  TCustomDrawStage = (
    cdPrePaint,
    cdPostPaint,
    cdPreErase,
    cdPostErase
  );

If your event handler is not getting called, that's not a Carbon issue, but an LCL one.

Thanks.

-Phil

Hansaplast

  • Hero Member
  • *****
  • Posts: 534
  • Tweaking4All.com
    • Tweaking4All
Re: TListView: onCustomDraw item, canvas.brush.color not working
« Reply #6 on: January 14, 2011, 05:09:41 am »
Thanks Phil as always for the explanation ... it's very much appreciated!  :)

I'm using 0.9.31 build date 2011-01-08.

I tried with both CustomDrawItem, AdvancedCustomDrawItem and even AdvancedCustomDraw:


procedure TfmMainForm.ListView1AdvancedCustomDrawItem(
  Sender: TCustomListView; Item: TListItem; State: TCustomDrawState;
  Stage: TCustomDrawStage; var DefaultDraw: Boolean);
begin
  Showmessage('a');
end;


Nothing happens. Ever.

p.s. I noticed that the property "OwnerDraw" has been commented out in the ComCtrls code. not sure how relevant that is ...

Phil

  • Hero Member
  • *****
  • Posts: 2750
Re: TListView: onCustomDraw item, canvas.brush.color not working
« Reply #7 on: January 14, 2011, 05:14:42 am »
Yeah, this thing does seem to be broke. I just tried it and couldn't get either event handler to be called. Nor could I get anything to display - when I tried entering items I started getting "list index out of bounds". Uh oh, untested code...

Could you use TListBox instead? I've never used TListView so I don't know what the salient differences are. If TListBox would do, you might look at TOvcVirtualListBox in the OrphPort package. It has an OnGetItemColor event that you can use to return both foreground color and background color for any item. That seems easier to figure out how to use.

Thanks.

-Phil

Hansaplast

  • Hero Member
  • *****
  • Posts: 534
  • Tweaking4All.com
    • Tweaking4All
Re: TListView: onCustomDraw item, canvas.brush.color not working
« Reply #8 on: January 14, 2011, 06:21:36 am »
Thanks Phil :)

I started out with a TListBox, but then ran into the problem that I actually need 2 columns with drag and drop, sorting, etc.

I'm experimenting with Lazarus, trying to create a little tool that helps me rename files, so one column shows the old name and the next column the new name and the rows obviously have to scroll vertically nicely in sync - I really don't know how to force listboxes to scroll in sync.

In the end the columns in TListBox do not exactly seem to work as in Delphi.
(Delphi has the char(9) option to tabulate which doesn't work in Lazarus)

After a lot of searching I found that most resort to TListView, so I tried that ...
Initially it looks good and is pretty fast, but the alternating line colors would be nice for readability.

Should I report this as a bug?
I did see a few more reports for non-Carbon widget sets.
I just don't want to be a bother either, since most developers do this work for free and I greatly appreciate them for doing this.

I did try VirtualTreeView, but it will not compile under Carbon. :(
It looked so good and promising :)

I'll try TOvcVirtualListBox tomorrow, see what it can do for me :)

As always; Thanks Phil! :)

Phil

  • Hero Member
  • *****
  • Posts: 2750
Re: TListView: onCustomDraw item, canvas.brush.color not working
« Reply #9 on: January 14, 2011, 04:23:13 pm »
Should I report this as a bug?
I did see a few more reports for non-Carbon widget sets.
I just don't want to be a bother either, since most developers do this work for free and I greatly appreciate them for doing this.

Yes, file a bug report in Mantis so there's a record.

Sounds like you really need something like the Cocoa NSOutlineView or NSBrowser:

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSOutlineView_Class/Reference/Reference.html

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSBrowser_Class/Reference/Reference.html

Thanks.

-Phil

Hansaplast

  • Hero Member
  • *****
  • Posts: 534
  • Tweaking4All.com
    • Tweaking4All
Re: TListView: onCustomDraw item, canvas.brush.color not working
« Reply #10 on: January 15, 2011, 05:04:18 am »
Well, I tried TOvcVirtualListBox, which looks exactly what I'd need, but it crashes when using the mouse wheel. Not just the app crashes,... it crashes Lazarus completely ...

I'd like to keep the app cross-platform, after all that is one of the reason why I'm experimenting with Lazarus :) So Cocoa NSOutlineView and NSBrowser are probably not a good choice :)
Thanks for the tip though!

I'll take a closer look at VirtualTreeView as soon as I find how to download from the svn (I'm still new to the svn thing).

Hansaplast

  • Hero Member
  • *****
  • Posts: 534
  • Tweaking4All.com
    • Tweaking4All
Re: TListView: onCustomDraw item, canvas.brush.color not working
« Reply #11 on: January 15, 2011, 05:13:41 am »
p.s. Posted the bug for the onCustomDrawItem event of the TListView.

Phil

  • Hero Member
  • *****
  • Posts: 2750
Re: TListView: onCustomDraw item, canvas.brush.color not working
« Reply #12 on: January 15, 2011, 10:07:10 pm »
Well, I tried TOvcVirtualListBox, which looks exactly what I'd need, but it crashes when using the mouse wheel. Not just the app crashes,... it crashes Lazarus completely ...

Thanks for testing. Same problem in several of the ported Orpheus controls. That should now be fixed in CCR SVN.

Points up the risk of porting antique Delphi packages. In this case, TurboPower used TMessage in their WMMouseWheel method instead of TWMMouseWheel, probably because the latter was not available in early versions of Windows. Delta is negative when you scroll down (note that it did not crash when scrolling up), yet they extract it from the message's wParam using HIWORD, then pass it as a SmallInt to DoOnMouseWheel. This doesn't work when range checking is turned on.

I don't use the scroll wheel very much. With my mouse, it's too easy to press down just a little bit too hard, which brings up Dashboard rather than scrolling.

Thanks.

-Phil

Hansaplast

  • Hero Member
  • *****
  • Posts: 534
  • Tweaking4All.com
    • Tweaking4All
Re: TListView: onCustomDraw item, canvas.brush.color not working
« Reply #13 on: January 16, 2011, 07:24:56 pm »
Thanks Phil - I really like how you always explain in great detail what is going on!

One of these days my experience might grow to a point where I can be helpful with actual coding :)