Lazarus

Programming => General => Topic started by: bobonwhidbey on June 13, 2021, 07:58:10 pm

Title: VirtualStringTree and PaintText
Post by: bobonwhidbey on June 13, 2021, 07:58:10 pm
I'm just starting to learn about all the amazing capabilities of TVirtualStringTree. I think a whole book might be written on it with all it's properties and options. I'd like to be able to "paint" the text area. I was hoping that something like this in onPaintText would work.
Code: Pascal  [Select][+][-]
  1.     Data := VST.GetNodeData(Node);
  2.     TargetCanvas.TextOut(x,y,Data.Text);

In this manner I could have some of the text in one color, and some in another. I also need to stop the text from being painted by the component.
Title: Re: VirtualStringTree and PaintText
Post by: winni on June 13, 2021, 08:20:36 pm
Hi!

Look at  the example page of VirtualTreeView and look at the onPaintText section:

https://wiki.freepascal.org/VirtualTreeview_Example_for_Lazarus (https://wiki.freepascal.org/VirtualTreeview_Example_for_Lazarus)

Winni
Title: Re: VirtualStringTree and PaintText
Post by: balazsszekely on June 13, 2021, 08:23:25 pm
@bobonwhidbey
Quote
I'm just starting to learn about all the amazing capabilities of TVirtualStringTree. I think a whole book might be written on it with all it's properties and options. I'd like to be able to "paint" the text area. I was hoping that something like this in onPaintText would work.
Try something like this:
Code: Pascal  [Select][+][-]
  1. type
  2.   PData = ^TData;
  3.   TData = record
  4.     FBold: Boolean;
  5.     FColored: Boolean;
  6.   end;
  7.  
  8. procedure TForm1.VSTPaintText(Sender: TBaseVirtualTree;
  9.   const TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
  10.   TextType: TVSTTextType);
  11. var
  12.   Data: PData;
  13. begin
  14.   if TextType <> ttNormal then
  15.     Exit;
  16.   Data := VST.GetNodeData(Node);
  17.   case Column of
  18.     -1: begin //if no columns are defined, the case is not needed
  19.          if Data^.FBold then
  20.            TargetCanvas.Font.Style := TargetCanvas.Font.Style + [fsBold];
  21.  
  22.          if Data^.FColored then
  23.            if Node = Sender.FocusedNode then
  24.              TargetCanvas.Font.Color := VST.Colors.SelectionTextColor
  25.            else
  26.              TargetCanvas.Font.Color := clRed;
  27.        end;
  28.     0: begin
  29.          //each column has different font
  30.        end;
  31.     end;
  32. end;
  33.  
  34. procedure TForm1.Button1Click(Sender: TObject);
  35. var
  36.   Node: PVirtualNode;
  37.   Data: PData;
  38. begin
  39.   Node := VST.AddChild(nil);
  40.   Data := VST.GetNodeData(Node);
  41.   Data^.FBold := True;
  42.   Data^.FColored := False;
  43.  
  44.   Node := VST.AddChild(nil);
  45.   Data := VST.GetNodeData(Node);
  46.   Data^.FBold := False;
  47.   Data^.FColored := True;
  48. end;    
Title: Re: VirtualStringTree and PaintText
Post by: bobonwhidbey on June 13, 2021, 08:45:21 pm
The examples and GetMem's approach seem to require that the text for the entire column must use the same font attributes. I want to have some text in one color/style and other parts of the text in another.
Title: Re: VirtualStringTree and PaintText
Post by: balazsszekely on June 13, 2021, 08:54:20 pm
@ bobonwhidbey
Quote
The examples and GetMem's approach seem to require that the text for the entire column must use the same font attributes. I want to have some text in one color/style and other parts of the text in another.
If you wish to have full control over the drawing then VirtualDrawTree is a much better option. You can find an example in the following post(see attachment):
https://forum.lazarus.freepascal.org/index.php/topic,37834.msg257241.html#msg257241

Please check the VDTDrawNode event.
TinyPortal © 2005-2018