Recent

Author Topic: annoying issue with implementing toggable state images for a TreeView  (Read 274 times)

jamie

  • Hero Member
  • *****
  • Posts: 3537
Ether I missed something in this control or it just isn't there but I find my self implementing the long way a process of be able to toggle the state image for the TreeNodes..

So if there is an event I am missing please tell me, if there isn't then why don't we have this ?
here is basically what I do, well in this particular project anyways
Code: Pascal  [Select][+][-]
  1. procedure Tprjtorpsf.TreeView1Click(Sender: TObject);
  2. var
  3.   PT:TPoint;
  4.   R:TRect;
  5.   N:TTreeNode;
  6. begin
  7.  With TTreeView(Sender) do
  8.   Begin
  9.    PT := Mouse.CursorPos;
  10.    PT := ScreenToClient(PT);
  11.    if (Selected <> Nil)And(Selected.Parent = Nil) Then
  12.       begin
  13.        R := TRect.Create(Selected.DisplayStateIconLeft, Selected.Top,Selected.DisplayTextleft,Selected.Bottom);
  14.        if R.Contains(PT) Then
  15.        Begin
  16.         Selected.StateIndex := Selected.StateIndex xor 1;
  17.         if Selected.StateIndex <> 0 Then
  18.          Begin
  19.            {We much uncheck others..Only One }
  20.            N := Items.GetFirstNode;
  21.            While (N <> Nil) do
  22.             Begin
  23.              If N <> Selected Then N.stateIndex := 0;
  24.              N := N.GetNextSibling;
  25.             end;
  26.          end;
  27.        end;
  28.       end;
  29.   end;
  30. end;
  31.  

A nice Event Like OnStateImage Click with the Node so I can alter it of course.

Am I missing something here ?
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 7551
Re: annoying issue with implementing toggable state images for a TreeView
« Reply #1 on: April 19, 2020, 12:12:00 am »
There's a function GetHitInfoAt(x,y) returning a set of htAbove, htBelow, htNowhere, htOnItem, htOnButton, htOnIcon, htOnIndent, htOnLabel, htOnRight, htOnStateIcon, htToLeft, htToRight, among them htOnStateIcon. Is it that what you are missing?
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

jamie

  • Hero Member
  • *****
  • Posts: 3537
Re: annoying issue with implementing toggable state images for a TreeView
« Reply #2 on: April 19, 2020, 12:17:37 am »
I'll look at it, it could aid in a bit but I use the Click event now and covert the mouse x,y over to client x,y..

I suppose I still need to do this but that would save me a step...

 Thanks I'll look at it..  :)
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 7551
Re: annoying issue with implementing toggable state images for a TreeView
« Reply #3 on: April 19, 2020, 12:28:56 am »
This works for me:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.TreeView1MouseDown(Sender: TObject; Button: TMouseButton;
  2.   Shift: TShiftState; X, Y: Integer);
  3. var
  4.   node: TTreeNode;
  5. begin
  6.   if (htOnStateIcon in Treeview1.GetHitTestInfoAt(X, Y)) then
  7.   begin
  8.     node := TreeView1.GetNodeAt(X, Y);
  9.     if node <> nil then
  10.       node.StateIndex := node.StateIndex xor 1;
  11.   end;
  12. end;  
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

jamie

  • Hero Member
  • *****
  • Posts: 3537
Re: annoying issue with implementing toggable state images for a TreeView
« Reply #4 on: April 19, 2020, 12:42:24 am »
  >:(

LOL

Are you looking over my shoulders because that is virtually identical to what I ended up with in that section of code..

 Btw, I think the help file needs up dating..

There is no "GetNodeAt(X,Y)" but "GetNodeATY(Y)" Instead...


Thanks for the help, I love using methods and code that are already in the soup bowl, don't like reinventing the wheel here

The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 7551
Re: annoying issue with implementing toggable state images for a TreeView
« Reply #5 on: April 19, 2020, 12:58:52 am »
Btw, I think the help file needs up dating..

There is no "GetNodeAt(X,Y)" but "GetNodeATY(Y)" Instead...
There is a GetNodeAtY(Y) in the protected section and a GetNodeAt(X,Y) in the public section of TCustomTreeView. The chm of 2.0.6 lists them correctly
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

jamie

  • Hero Member
  • *****
  • Posts: 3537
Re: annoying issue with implementing toggable state images for a TreeView
« Reply #6 on: April 19, 2020, 01:14:56 am »
You are absolutely correct, I got lost in the help file there... Sorry about that …


 Its a good thing someone around here is still awake!  :)
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018