Recent

Author Topic: TreeView Default editor-how to access it for QUE_BANNER text ?  (Read 443 times)

jamie

  • Hero Member
  • *****
  • Posts: 1993
TreeView Default editor-how to access it for QUE_BANNER text ?
« on: September 15, 2019, 05:41:14 pm »

 The default editor you get for the Cells of a node when in editing mode, I need to set the QUE_BANNER text or
in the case of a TEDIT the HintText.

   Unless I am overlooking something I don't see a Accessible property in the TreeView control to get at the editor object itself nor do I see the option property?

  Am I blind ?

jamie

  • Hero Member
  • *****
  • Posts: 1993
Re: TreeView Default editor-how to access it for QUE_BANNER text ?
« Reply #1 on: September 15, 2019, 06:11:33 pm »
It seems I can do this but it isn't Cross platform ..

TVM_GETEDITCONTROL returns the handle then I can use the SendMessage(….. to
set the Que_Banner text..

  Shouldn't there be a property in there for this that works on the other widgets too ?

ASerge

  • Hero Member
  • *****
  • Posts: 1408
Re: TreeView Default editor-how to access it for QUE_BANNER text ?
« Reply #2 on: September 15, 2019, 09:24:05 pm »
  Am I blind ?
It's protected. Helper can help:
Code: Pascal  [Select]
  1. type
  2.   TTreeViewHelper = class helper for TTreeView
  3.   public
  4.     procedure SetCueBanner(const AText: string);
  5.   end;
  6.  
  7. procedure TForm1.TreeView1Editing(Sender: TObject; Node: TTreeNode; var AllowEdit: Boolean);
  8. begin
  9.   TreeView1.SetCueBanner('Test');
  10. end;
  11.  
  12. procedure TTreeViewHelper.SetCueBanner(const AText: string);
  13. begin
  14.   if Assigned(FEditor) then
  15.     FEditor.TextHint := AText;
  16. end;

jamie

  • Hero Member
  • *****
  • Posts: 1993
Re: TreeView Default editor-how to access it for QUE_BANNER text ?
« Reply #3 on: September 15, 2019, 11:24:43 pm »
Ok, I can try that.

But why is that protected ? Wouldn't you think that would be a good feature to expose?


EDIT:

  Back again, Well it seems there is a reason why it's not exposed. Cause it does not work..
 
  FEditor is always NIL, Most likely because windows has its own editor for this.

 I am sure it works in the other Widget sets since they most likely need to implement the editor.
 If it works in the other widgets then maybe a work around for all can be made somehow ?
  I guess it would have to be in the Interface code so the base code will have no conditions in it.

EDIT 2

 back again, it seems I spoke too soon, it does not work on the first go around.
the editor is late coming in. I'll have to see if I can figure that out.
 
  It seems the editor isn't yet functional before the OnEditing is called.


« Last Edit: September 15, 2019, 11:55:12 pm by jamie »

jamie

  • Hero Member
  • *****
  • Posts: 1993
Re: TreeView Default editor-how to access it for QUE_BANNER text ?
« Reply #4 on: September 16, 2019, 12:42:32 am »
Ok, I found the problem area in "treeview.inc" at ~3367 ..
Code: Pascal  [Select]
  1. procedure TCustomTreeView.BeginEditing(ANode: TTreeNode);
  2. var
  3.   ARect: TRect;
  4. begin
  5.   //DebugLn(['TCustomTreeView.BeginEditing tvsIsEditing=',tvsIsEditing in FStates,' Selected=',Selected<>nil]);
  6.   if (tvsIsEditing in FStates) or (ANode=nil) then exit;
  7.  
  8.   // EDITOR NEEDS to be created here!   <<< Please NOTE
  9.  
  10.   if (not CanEdit(ANode)) or (not ANode.Visible) then exit;
  11.   // if we are asked to edit another node while one is already being edited then
  12.   // stop editing that node
  13.   if FEditingItem <> nil then
  14.     EndEditing;
  15.   FEditingItem := ANode;
  16.   // make node visible (this will cancel editing, so call this first)
  17.   EnsureNodeIsVisible(ANode);
  18.  
  19.   Include(FStates,tvsIsEditing);
  20.   if FEditor=nil then begin  // Editor should not be created here, too late..!
  21.      FEditor:=TEdit.Create(Self);
  22.     FEditor.OnEditingDone:=@EditorEditingDone;
  23.     FEditor.OnKeyDown:=@EditorKeyDown;
  24.   end;
  25.   ARect:=Rect(Max(BorderWidth,ANode.DisplayTextLeft),ANode.Top-ScrolledTop,
  26.               ClientWidth-BorderWidth,ANode.Bottom-ScrolledTop);
  27.   FEditor.BoundsRect:=ARect;
  28.   FEditor.AnchorParallel(akLeft,ARect.Left,Self);
  29.   FEditor.AnchorParallel(akRight,BorderWidth,Self);
  30.   FEditor.Visible:=true;
  31.   FEditor.Parent:=Self;
  32.   FEditor.Text:=ANode.Text;
  33.   FEditor.SelectAll;
  34.   FEditor.SetFocus;
  35. end;                              
  36.  
  37.  

Please refer to lines #8 and #20

jamie

  • Hero Member
  • *****
  • Posts: 1993
Re: TreeView Default editor-how to access it for QUE_BANNER text ?
« Reply #5 on: September 16, 2019, 01:07:07 am »
I wrote a Class over ride locally instead so I could override the beginEditing.
Code: Pascal  [Select]
  1. TTreeView = Class(ComCtrls.TTreeView)
  2.   Procedure SetQueBanner(Const AText:String);
  3.   Procedure BeginEditing(ANode:TTreeNode); Override;
  4. end;
  5.  
  6.  implementation
  7.  
  8. {$R *.lfm}
  9.  Procedure TTreeView.BeginEditing(ANode:TTreeNode);
  10. Begin
  11. If Feditor = Nil Then
  12. Begin
  13.  FEditor:=TEDIT.Create(Self);//Editor must be created early..
  14.  FEditor.OnEditingDone:=@EditorEditingDone;
  15.  FEditor.OnKeyDown:=@EditorKeyDown;
  16. End;
  17.  Inherited BeginEditing(Anode);                                                                                                          End;          
  18.  
and the rest is as you original suggested but with using a class instead, because I was unable to override the method with a helper.

  This now works, maybe we can suggest it to be fixed in the next release and also add the HintText Property ?
« Last Edit: September 16, 2019, 01:15:59 am by jamie »

ASerge

  • Hero Member
  • *****
  • Posts: 1408
Re: TreeView Default editor-how to access it for QUE_BANNER text ?
« Reply #6 on: September 16, 2019, 10:18:22 pm »
back again, it seems I spoke too soon, it does not work on the first go around.
the editor is late coming in. I'll have to see if I can figure that out.
  It seems the editor isn't yet functional before the OnEditing is called.
Yeah, and I don't see what the problem is?

jamie

  • Hero Member
  • *****
  • Posts: 1993
Re: TreeView Default editor-how to access it for QUE_BANNER text ?
« Reply #7 on: September 16, 2019, 10:41:06 pm »
The problem is the first time you go into edit mode and need to set the Editor.hintText the editor is NIL if you are trying to do this via the OnEditing event.

 The Event is called before the TEDIT is created in the background so the first time you select editing there is a NIL editor so it never gets the chance to set the HintText inside the OnEditing Event.
 
 Once the editor has been created it works ok but you miss the first go around.

 if you look above as to what I did you can see the issue and how I worked around it.

 Simply put, the Editor is being created too late in the BeginEditing event..

 I showed the code as it is now in "Treeview.inc" at line #3367 or there abouts, and I also showed the comment about what needs to be done..
 
 I can put this in as a fix request but I know it will go NO WHERE>

 I can supply an example test app of the problem if you wish?


ASerge

  • Hero Member
  • *****
  • Posts: 1408
Re: TreeView Default editor-how to access it for QUE_BANNER text ?
« Reply #8 on: September 17, 2019, 07:13:15 pm »
The problem is the first time you go into edit mode and need to set the Editor.hintText the editor is NIL if you are trying to do this via the OnEditing event.
Okay, thanks. So my example only works the second time.
I suggest to make feature request in bugtracker, to add a method that will create FEditor unconditionally.

jamie

  • Hero Member
  • *****
  • Posts: 1993
Re: TreeView Default editor-how to access it for QUE_BANNER text ?
« Reply #9 on: September 20, 2019, 12:17:55 am »
Ok, his is the source file I will  present as a concept.
Code: Pascal  [Select]
  1. {$mode objfpc}{$H+}
  2. {$ModeSwitch TypeHelpers}
  3.  
  4. interface
  5.  
  6. uses
  7.   Windows, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, StdCtrls, Classes;
  8.  
  9. type
  10.  
  11.   { TForm1 }
  12.   TTreeView = class(ComCtrls.TTreeView)      {Local class override to correct issues }
  13.     procedure SetQueBanner(const AText: string);
  14.     procedure BeginEditing(ANode: TTreeNode); override;
  15.   end;
  16.  
  17.   TForm1 = class(TForm)
  18.     TreeView1: TTreeView;
  19.     procedure TreeView1Editing(Sender: TObject; Node: TTreeNode;
  20.       var AllowEdit: boolean);
  21.   private
  22.  
  23.   public
  24.  
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33. procedure TTreeView.BeginEditing(ANode: TTreeNode);
  34. begin
  35.   if Feditor = nil then
  36.   begin
  37.     FEditor := TEDIT.Create(Self);//Editor must be created early..
  38.     FEditor.OnEditingDone := @EditorEditingDone;
  39.     FEditor.OnKeyDown := @EditorKeyDown;
  40.   end;
  41.   inherited BeginEditing(Anode);
  42. end;
  43.  
  44. procedure TtreeView.SetQueBanner(const AText: string);
  45. begin
  46.   if Assigned(Feditor) then
  47.     Feditor.TextHint := AText;
  48. end;
  49.  
  50. { TForm1 }
  51.  
  52. procedure TForm1.TreeView1Editing(Sender: TObject; Node: TTreeNode;
  53.   var AllowEdit: boolean);
  54. begin
  55.   {Line@ ~3367 in TreeView.inc creates the editor too late }
  56.   {BeginEditng Method is overriden to correct for this }
  57.   {just to demo the effects of the correction}
  58.   TreeView1.SetQueBanner('Field Can not be empty');
  59. end;
  60.  
  61. end.                                        
  62.