View Issue Details

IDProjectCategoryView StatusLast Update
0025397LazarusLCLpublic2021-02-12 14:34
ReporterRichard Mace Assigned Towp  
PrioritynormalSeverityminorReproducibilityalways
Status assignedResolutionopen 
PlatformWindowsOSXP 
Summary0025397: Font not becoming bold on OwnerDraw
DescriptionIf you specify the font to be bold and colour, it on displays the colour not bold.
  Sender.Canvas.Font.Style := [fsBold];
  Sender.Canvas.Font.Color := clGreen;
Steps To ReproduceAs per project
TagsNo tags attached.
Fixed in Revision64204
LazTarget2.2
WidgetsetWin32/Win64
Attached Files

Relationships

related to 0025418 resolvedZeljan Rikalo Listview OwnerDraw not working in Linux 

Activities

Richard Mace

2013-12-05 12:47

reporter  

test.zip (128,355 bytes)

Zeljan Rikalo

2013-12-12 17:10

developer   ~0071880

This is bad named issue. OwnerDraw is false in your case, and if it's true then you should use OnDrawItem (like embarcadero docs says), not OnCustomDrawItem or OnAdvancedCustomDraw....etc

wp

2013-12-14 17:42

developer  

D7.png (27,162 bytes)   
D7.png (27,162 bytes)   

wp

2013-12-14 17:43

developer   ~0071894

I converted the demo to Delphi 7, and it does show the colored items in bold although OwnerDraw is false -- see D7.png

Zeljan Rikalo

2013-12-14 18:03

developer   ~0071895

I'm just making this issue subject clear. OwnerDraw have nothing to do with this issue.

Richard Mace

2013-12-17 11:24

reporter   ~0071931

I have OwnerDraw set to false, and I agree, it's not the best named fault so sorry for that. Have you been able to test in XP?

Richard Mace

2014-09-15 13:59

reporter   ~0077255

Hi, does anybody need any further clarification from me, or I am doing something wrong?
Would really like to try and assist with getting this bug fixed.

Richard

Zeljan Rikalo

2020-04-01 10:53

developer   ~0121824

Detached myself from this issue. It's for someone with deep knowledge of msdn api.

jamie philbrook

2020-12-11 22:03

reporter   ~0127546

I have no issue using it as is... when used properly it works

I did some leg work on the issue and found the OS basically will only accept the foreground and background color changes but the font is the control's font not the canvas.
and don't bother trying to change the control.font….. cause it will loop if you do this within the drawing events.
I think it should be closed.

wp

2020-12-11 23:48

developer   ~0127548

Did you try the sample project in the first post? I don't see anything which is not "used properly". If you know better please show how it must be done.

jamie philbrook

2020-12-12 00:55

reporter   ~0127550

Yes of course I did..
 
I tracked it all the way into the widget set where it does the drawing there..

using it in the manner they are you can only set the Forground and Background colors of the text, also the caption itself and other attributes..

The HDC handle is restored from a saved condition and the Windows interface only takes the Color information from the Font.color and Brush.color for background info's
There is no recording of the FOnt.Style so you end up with the ListVIew1.Font.Style instead.
 
 So In the end, the control when doing its own drawing ends up using the Font.Style of the ListView Control which has been assigned to it at the birth of the control.

when ever you set this property during execution it prompts a series of messages and one of them is the repaint update and if we are already in the repaint update it gets stuck.

if you implement the OnDrawItem you can fully draw the items yourself in place with no help from the widget, and that includes setting the Bold on the font because that is the canvas in play at the time.

 To get that to work as the original intent a lot of work is needed in the widget

wp

2020-12-12 11:30

developer   ~0127555

In Delphi the exact code of the demo project does produce bold items. Therefore, this is a valid bug and should not be closed.

Bart Broersma

2020-12-12 14:20

developer   ~0127559

(Unless the Delphi behaviour is a bug in itself)

jamie philbrook

2020-12-12 14:36

reporter   ~0127560

Ok, its in the WS32CustomListView,Inc

The Device Context for the Listview in the CustomDraw event needs to be updated I guess without a flurry of messages coming back..

I'll poke at it.

jamie philbrook

2020-12-12 15:24

reporter   ~0127562

Here is a patch file. it seems to work,.

Simple one liner in the widget
win32wscustomlistview.inc.patch (561 bytes)   
Index: win32wscustomlistview.inc
===================================================================
--- win32wscustomlistview.inc	(revision 64198)
+++ win32wscustomlistview.inc	(working copy)
@@ -228,6 +228,7 @@
       DrawInfo^.clrText := ColorToRGB(ALV.Canvas.Font.Color);
       DrawInfo^.clrTextBk := ColorToRGB(ALV.Canvas.Brush.Color);
     end;
+    SelectObject(DrawInfo^.nmcd.hdc, ALV.Canvas.Font.Reference.Handle);//Update font incase of chages JP.
     ALV.Canvas.Handle := OldDC;
 
     for ResultFlag := Low(ResultFlag) to High(ResultFlag) do

wp

2020-12-12 16:50

developer   ~0127565

Thank you. Almost... With the patch applied, run the demo project. Initially items 4 and 5 as well as their subitems are bold and colored. Now click on item3 --> suddenly SubItem3 becomes green and bold as well.

Bart Broersma

2020-12-12 17:37

developer   ~0127567

The example has OwnerDraw = False.

From the Delphi docs:
http://docwiki.embarcadero.com/Libraries/Sydney/en/Vcl.ComCtrls.TListView_Events
"OnCustomDrawItem: Occurs when an item in an owner-draw list view must be rendered."

So this should only work if OwnerDraw = True.
If it works (in Delphi) with OwnerDraw = False, then this is a Delphi bug.

jamie philbrook

2020-12-12 17:41

reporter   ~0127568

Last edited: 2020-12-12 17:44

View 2 revisions

I just checked that on two PC's both 64. Old laz and trunk. They work perfectly..
there is no strange effects taking place here

However, I just decided to DelecteObject( SelectObject……) and It does do that but the patch didn't delete any object...
 
so what you are telling me the object is getting deleted but I am not doing that what I posted and it works perfectly here.

I'll dig in deeper..

Edit:
  The demo originally didn't have the OnwerDraw checked but the event fires anyways. could be some difference there

wp

2020-12-12 18:08

developer   ~0127570

When I set OwnerDraw=true in Delphi the example does not work at all any more. But when I add an OnCustomDrawSubItem handler with the same code as the OnCustomDrawITem handler it works again. The situation is similar to the OnPrepareCanvas event of TStringGrid where the user changes the canvas immediately before drawing, but the grid finally executes the drawing. The OwnerDraw property seems to tell Delphi that the user wants to draw everything on his own. Not very logical, but one of the many Delphi inconsistencies.

It has nothing to do with the issues reported here: The user wants to draw some items in colored and bold font, Delphi does it like that, in old (D7) and new (XE10.3) versions. , but Lazarus only respects the color. As jamie described the API gives direct access only to the font color, not to the font style. But since Delphi can draw the font style with exactly the same code Lazarus should be able to draw it either.

OK, maybe we can discuss whether this is a "bug" or not. At least it breaks Delphi compatibility, and I had several cases when I wanted to draw ListView items in bold.

jamie philbrook

2020-12-12 18:14

reporter   ~0127571

Last edited: 2020-12-12 18:21

View 2 revisions

So I just found another problem that is not related directly

if you don't have any of the events set so that none of them get fired, norm use of a Tlistview but have the OnData checked and a handler for it, it will crash with an index bounds error(5)
the second time you push the button.

from what I see here, it does not even reach the event handler for the OnData..

do I need to do a complete rebuild here ?

I just reverted and the crash is still there , it has nothing to do with my changes.. I think the other parts of this control need to be fixed before we fix this report

wp

2020-12-12 19:02

developer   ~0127573

OnData? This has nothing to do with the current issue. No idea what you are doing, but I just checked that virtual mode (which needs OnData) is working correctly (https://github.com/wp-xyz/LazSamples/tree/master/lcl/listview_virtualmode).

jamie philbrook

2020-12-12 20:23

reporter   ~0127576

Last edited: 2020-12-12 21:06

View 3 revisions

Please test the theory with the sample app...
Regardless of what other controls are doing, with this demo and enable OnData it crashes..

I just tried is on a old Laz 2.0.0, same deal;

I did have to change the Integer type in the BUTTON1 event to Int64 because its loading the object of the stringlist with the index number and that does not work well in 64 bit land unless you use Int64.
 SubItems.AddObject('SubItem' + IntToStr(I),TObject(I)); <<<<<< I needs to be int64 for this to compile.

other than that, the fix I offered here works perfectly but you say it does not.. so there we are.
I'll get back to it.

EDIT:
 I figured out how to cause the crash...
OnData := True; // You don't need any event handler of any of them.
 Using this demo, Click once to populate list, all is fine except for of course, the original bug..
Then Click the BUtton again, which gets to the line where it does the ITEMS.CLEAR and it crashes with an index out of bounds.

This is clearly a bug.. which can be open in another report later.

jamie philbrook

2020-12-13 02:03

reporter   ~0127581

Please try this patch, also please try the app with a Resource monitor please.
It seems not all the tools I have here works on Win10.
--
This patch is basically the same but done in a different place and also takes an extra step to get the control to switch over to the new font without waiting for notify message.
win32wscustomlistview.inc-2.patch (687 bytes)   
Index: win32wscustomlistview.inc
===================================================================
--- win32wscustomlistview.inc	(revision 64199)
+++ win32wscustomlistview.inc	(working copy)
@@ -227,6 +227,11 @@
     begin
       DrawInfo^.clrText := ColorToRGB(ALV.Canvas.Font.Color);
       DrawInfo^.clrTextBk := ColorToRGB(ALV.Canvas.Brush.Color);
+      if GetCurrentObject(DrawInfo^.nmcd.hdc,OBJ_FONT)<>(ALV.Canvas.Font.Reference.Handle) Then
+      Begin
+        SelectObject(DrawInfo^.nmcd.hdc,ALV.Canvas.Font.Handle);
+        SendMessage(ALV.Handle, WM_SETFONT, ALV.Canvas.Font.Reference.Handle, 0{no repaint});
+      end;
     end;
     ALV.Canvas.Handle := OldDC;
 

wp

2020-12-13 12:47

developer   ~0127585

Last edited: 2020-12-13 12:48

View 2 revisions

Sorry, the same issue: Screenshot 2597-1.png shows the demo form after program start --> correct. I click on "Item 3" --> "SubItem3" changes font color and style (2597-2.png) --> wrong.

I wonder why you don't see these. I am on Win 10/64 bit, my development system is Laz trunk/fpc3.2.0 / 32bit. I am just compiling and running the test application of the first post (test.zip).
25397-2.png (4,220 bytes)   
25397-2.png (4,220 bytes)   
25397-1.png (4,025 bytes)   
25397-1.png (4,025 bytes)   

jamie philbrook

2020-12-13 15:48

reporter   ~0127587

To begin with, The test app is flawed to start with..
the subitems should be done in the SubItem Draw event, Not the itemDraw event...
But what I don't understand is yours changing color like that when you select it, it does not happen here on a Home Win10, proWin10 and a 32 bit tablet Win10.

I can test vista too but I am sure the results will be the same..
Attached is a modified test app that actually implements the SubItem draw at the correct moment.

Please examine that code and test run it against the patch I offered.
project1.zip (128,832 bytes)

wp

2020-12-13 17:48

developer   ~0127592

Sorry - I had fogotten about the modifications that I myself had made to the test program (beginning of Alzheimer?) Much better now, but there is still an issue, also with your demo: Run the demo, click the button, then on Item3 and finally click the button again. Now the listview headers become bold...

jamie philbrook

2020-12-13 18:27

reporter   ~0127594

Yes I saw that but it also does that in Delphi 10.x too.

The Font has changed and it effects the rest of the control.

So when you highlight the item, only that line is getting repainted and thus the font is getting changed then when you hover over the headers it repaints with the new fonts.

Personally I would prefer not even having this feature and just simple do the Drawing within the custom events where It only changes the temp canvas. that is or was the original intention.
Setting the DefaultDrawing := False will prevent the OS from drawing it.

 Restoring the font does not look like its a viable option here..

Windows does not address the issues of Font selection per item, only foreground and backgrounds, otherwise its a hack..

wp

2020-12-13 19:57

developer   ~0127596

> Yes I saw that but it also does that in Delphi 10.x too.
Not for me...

I do not fully understand this control. But it looks to me that there are several phases. Can't the original font be restored in the "post-paint" phase, or at the end of the wmPaint message handler?

jamie philbrook

2020-12-15 02:42

reporter   ~0127615

Please try this patch..
this is actually much simpler..
commented one line and added another else where.
win32wscustomlistview.inc-3.patch (721 bytes)   
Index: win32wscustomlistview.inc
===================================================================
--- win32wscustomlistview.inc	(revision 64202)
+++ win32wscustomlistview.inc	(working copy)
@@ -200,7 +200,7 @@
     end;
 
     OldDC := ALV.Canvas.Handle;
-    ALV.Canvas.Handle := DrawInfo^.nmcd.hdc;
+    //ALV.Canvas.Handle := DrawInfo^.nmcd.hdc;  //jp
     ALV.Canvas.Font.Assign(ALV.Font);
     ALV.Canvas.Brush.Assign(ALV.Brush);
 
@@ -227,6 +227,7 @@
     begin
       DrawInfo^.clrText := ColorToRGB(ALV.Canvas.Font.Color);
       DrawInfo^.clrTextBk := ColorToRGB(ALV.Canvas.Brush.Color);
+      SelectObject(DrawInfo^.nmcd.hdc, ALV.Canvas.Font.Handle); //jp
     end;
     ALV.Canvas.Handle := OldDC;
 

wp

2020-12-15 11:14

developer   ~0127619

Perfect. Excellent detective work!

I don't know if the original reporter is still around after this long time: Please test and close if ok.

wp

2021-02-12 14:34

developer   ~0128909

Reverted r64204 because it breaks custom drawing.

Issue History

Date Modified Username Field Change
2013-12-05 12:47 Richard Mace New Issue
2013-12-05 12:47 Richard Mace File Added: test.zip
2013-12-06 10:44 Marc Weustink Project Lazarus CCR => Lazarus
2013-12-06 11:46 Zeljan Rikalo Assigned To => Zeljan Rikalo
2013-12-06 11:46 Zeljan Rikalo Status new => assigned
2013-12-11 13:23 Reinier Olislagers Relationship added related to 0025418
2013-12-12 17:10 Zeljan Rikalo LazTarget => -
2013-12-12 17:10 Zeljan Rikalo Note Added: 0071880
2013-12-12 17:10 Zeljan Rikalo Status assigned => feedback
2013-12-14 17:42 wp File Added: D7.png
2013-12-14 17:43 wp Note Added: 0071894
2013-12-14 18:03 Zeljan Rikalo Note Added: 0071895
2013-12-17 11:24 Richard Mace Note Added: 0071931
2013-12-17 11:24 Richard Mace Status feedback => assigned
2014-09-15 13:59 Richard Mace Note Added: 0077255
2020-04-01 10:53 Zeljan Rikalo Assigned To Zeljan Rikalo =>
2020-04-01 10:53 Zeljan Rikalo Status assigned => new
2020-04-01 10:53 Zeljan Rikalo Note Added: 0121824
2020-12-11 16:32 wp Category Custom Drawn => LCL
2020-12-11 16:32 wp Widgetset Win32/Win64 => Win32/Win64
2020-12-11 22:03 jamie philbrook Note Added: 0127546
2020-12-11 23:48 wp Note Added: 0127548
2020-12-12 00:55 jamie philbrook Note Added: 0127550
2020-12-12 11:30 wp Note Added: 0127555
2020-12-12 14:20 Bart Broersma Note Added: 0127559
2020-12-12 14:36 jamie philbrook Note Added: 0127560
2020-12-12 15:24 jamie philbrook Note Added: 0127562
2020-12-12 15:24 jamie philbrook File Added: win32wscustomlistview.inc.patch
2020-12-12 16:50 wp Note Added: 0127565
2020-12-12 17:37 Bart Broersma Note Added: 0127567
2020-12-12 17:41 jamie philbrook Note Added: 0127568
2020-12-12 17:44 jamie philbrook Note Edited: 0127568 View Revisions
2020-12-12 18:08 wp Note Added: 0127570
2020-12-12 18:14 jamie philbrook Note Added: 0127571
2020-12-12 18:21 jamie philbrook Note Edited: 0127571 View Revisions
2020-12-12 19:02 wp Note Added: 0127573
2020-12-12 20:23 jamie philbrook Note Added: 0127576
2020-12-12 21:05 jamie philbrook Note Edited: 0127576 View Revisions
2020-12-12 21:06 jamie philbrook Note Edited: 0127576 View Revisions
2020-12-13 02:03 jamie philbrook Note Added: 0127581
2020-12-13 02:03 jamie philbrook File Added: win32wscustomlistview.inc-2.patch
2020-12-13 12:47 wp Note Added: 0127585
2020-12-13 12:47 wp File Added: 25397-1.png
2020-12-13 12:47 wp File Added: 25397-2.png
2020-12-13 12:48 wp Note Edited: 0127585 View Revisions
2020-12-13 15:48 jamie philbrook Note Added: 0127587
2020-12-13 15:48 jamie philbrook File Added: project1.zip
2020-12-13 17:48 wp Note Added: 0127592
2020-12-13 18:27 jamie philbrook Note Added: 0127594
2020-12-13 19:57 wp Note Added: 0127596
2020-12-15 02:42 jamie philbrook Note Added: 0127615
2020-12-15 02:42 jamie philbrook File Added: win32wscustomlistview.inc-3.patch
2020-12-15 11:07 wp Assigned To => wp
2020-12-15 11:07 wp Status new => assigned
2020-12-15 11:14 wp Status assigned => resolved
2020-12-15 11:14 wp Resolution open => fixed
2020-12-15 11:14 wp Fixed in Revision => 64202
2020-12-15 11:14 wp LazTarget - => 2.2
2020-12-15 11:14 wp Widgetset Win32/Win64 => Win32/Win64
2020-12-15 11:14 wp Note Added: 0127619
2021-02-12 14:29 wp Fixed in Revision 64202 => 64204
2021-02-12 14:29 wp Widgetset Win32/Win64 => Win32/Win64
2021-02-12 14:34 wp Status resolved => assigned
2021-02-12 14:34 wp Resolution fixed => open
2021-02-12 14:34 wp Note Added: 0128909