Recent

Author Topic: [hacked for now] Changing Enable on items during a MenuPopUp bugged!  (Read 1007 times)

jamie

  • Hero Member
  • *****
  • Posts: 7490
Code: Pascal  [Select][+][-]
  1. procedure TTemplateform.TreeViewPopUpPopup(Sender: TObject);
  2. Var
  3.   M:TMenuItem;
  4. begin
  5.   For M in TreeViewPopUp.Items do
  6.   M.Enabled := TreeView1.Items.Count <> 0;
  7. end;                                    
  8.  
consider this, a TPopUp assigned to a TreeView

I need to scan the MenuItems to set their ENABLED property on condition of a loaded Treeview.
WHen I do this, It must be placing the Menu behind the form because after this, I can no longer interact with the form
because all focus is pointing to the Popup or something, you get the nice Ding Dong bells but other forms will react.

 I don't think its restricted to a TreeView, just putting it on a form and assigning it to the form should be enough.

 Maybe a OnBeforePopUp is needed ?

Jamie
« Last Edit: December 14, 2025, 06:59:49 pm by jamie »
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7490
Re: Changing Enable on items during a MenuPopUp bugged!
« Reply #1 on: December 14, 2025, 05:13:01 pm »
I just did this in the section where I need this.

Maybe this can be added at some point ?

It fixes the problem by allowing me to modify the menu prior to popping it open.

Code: Pascal  [Select][+][-]
  1.  TPopUpMenu = Class(Menus.TPopUpMenu)
  2.    Private
  3.    fOnBeforePopUp:TNotifyEvent;
  4.    procedure DoPopUp(Sender:TObject);Override;
  5.    Public
  6.    property OnBeforePopUp:TNotifyEvent read fOnBeforePopUp write fOnBeforePopUp;
  7.  end;  
  8. -- in code land;
  9. procedure TPopUpMenu.DoPopUp(Sender: TObject);
  10. begin
  11.   if Assigned(fOnBeforePopUp) Then
  12.     FOnBeforePopUp(Sender);
  13.   inherited DoPopUp(Sender);
  14. end;  
  15.  
  16. --- my event that I assign in the OnCreate of the form
  17. procedure TTemplateform.TreeViewPopUpBeforePopup(Sender: TObject);
  18. Var
  19.   M:TMenuItem;
  20. begin
  21.   For M in TreeViewPopUp.Items do
  22.    M.Enabled := TreeView1.Items.Count <>0;
  23. end;                                                                                        
  24.  

is it possible to get this added ?

Jamie
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7490
Re: Changing Enable on items during a MenuPopUp bugged!
« Reply #2 on: December 14, 2025, 06:58:12 pm »
It appears That I may have made a mistake, I didn't specify the M.Enable = etc

But even then, It miss behaves..

It would be nice to have a OnBeforePopUp.

Jamie


The only true wisdom is knowing you know nothing

mtrsoft

  • Jr. Member
  • **
  • Posts: 63
Re: [hacked for now] Changing Enable on items during a MenuPopUp bugged!
« Reply #3 on: December 14, 2025, 08:32:07 pm »
You can use the OnPopUp method / event.

It occurs before the popup menu is displayed and changes to the enable, visible, etc. properties can be in this event will be reflected in the displayed popup.

jamie

  • Hero Member
  • *****
  • Posts: 7490
Re: [hacked for now] Changing Enable on items during a MenuPopUp bugged!
« Reply #4 on: December 14, 2025, 09:00:23 pm »
You are correct because a simple test in 64bit mode says it works as it should, but I do that in my current application, which is kind of large in 32-bit mode, and for some reason the menu misbehaves?

 The hack that I did corrected that. Maybe I need to dig into my app deeper for some un-expected menu access, there is a lot going on in that app. It has DSP FFT constantly running, painting images and also the user is most of the time heavily active on the screen with the rest of the application, especially in the Template Editor where it really sucking some resources.

 Between the TWain, VFW Capture, FTP handling along with the DSP running it puts a strain on the I5 processor and I've noticed in areas where messages from the GDI at times gets stuffed up because I put emphases on the DSP part for the resources and it could be affecting the menus since those are also message driven.

Thanks.
Jamie


The only true wisdom is knowing you know nothing

Zvoni

  • Hero Member
  • *****
  • Posts: 3227
Re: [hacked for now] Changing Enable on items during a MenuPopUp bugged!
« Reply #5 on: December 15, 2025, 09:25:15 am »
Irrespective of your issue:
Why are you checking Items.Count for every Menu-Item?
Result will always be the same, irrespective if you have 3, 7 or 21 MenuItems

Check it once (save in boolean), and be done with it
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

jamie

  • Hero Member
  • *****
  • Posts: 7490
Re: [hacked for now] Changing Enable on items during a MenuPopUp bugged!
« Reply #6 on: December 15, 2025, 01:24:48 pm »
I did that out of laziness, it's simpler for me instead of adding the extra logic. The menu isn't that large anyways.

Putting that aside, I did find something interesting when trying write the Nodes of a TreeView out to an XML using the XMLPropStorage. My XML file is getting messed even though the nodes are reading back correctly, strange?

 The menu is attached to this same Treeview in question.

 So apparently, LineFeeds, CR , TABS get inserted in the stream which I don't think XML likes :o

 Looking at examples online, very few at that, the SearchReplace call was made to remove these but that isn't the whole story, you actually need those in return for proper population and currently although they repopulate, I think it is causing and unseen memory flood.

 I just did some test code and also found that TStringStream.ReadAnsistring; seems not to stop when I hits the end of the stream? So, I have to use the overload that needs a size index.

 This of course, seems to be a bug in the TStringStream.

Below is my current test code which I will implement to save the nodes to a XML file using the XMLPropStorage.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Var
  3.   S:TStringStream;
  4.   SS:String;
  5. begin
  6.   S:= TStringStream.Create;
  7.   Treeview1.SaveToStream(S);
  8.   S.Position := 0;
  9.   SS:= S.ReadAnsiString(S.Size); //S.ReadAnsiString does not stop at EOS.
  10.   SS := EncodeStringbase64(SS);
  11.   SS:= DecodeStringBase64(SS);
  12.   S.Free;
  13. end;                        
  14.  

I used the debugger to inspect the SS contents for validity both ways to ensure correctness.

This allows for restoring the line ending and tabs, it also makes the output smaller so its a win win.

I will see how this step plays out and hope it clears up my mysterious issues.

Thanks.
Jamie

The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7490
Re: [hacked for now] Changing Enable on items during a MenuPopUp bugged!
« Reply #7 on: December 15, 2025, 04:40:15 pm »
I will drop the TTreeView code here that I used and read and write from the XMLPropStorage of the Items in the control.

BTW, it appears that it fixed my Menu issues, I can go back to the original method, must have been leaking code! :o

Code: Pascal  [Select][+][-]
  1. procedure TForm1.XMLPropStorage1RestoreProperties(Sender: TObject);
  2. var
  3.   S:TStringStream;
  4. begin
  5.   S := TStringStream.Create;
  6.   S.WriteAnsistring(DecodeStringBase64((XMLpropStorage1.DoReadString('LISTView1','ListView1_Items',''))));
  7.   S.Position:=0;
  8.   TreeView1.LoadFromStream(S);
  9.   S.Free;
  10. end;
  11.  
  12. procedure TForm1.XMLPropStorage1SaveProperties(Sender: TObject);
  13. Var
  14.  S:TstringStream;
  15. begin
  16.   S:= TStringStream.Create;
  17.   Treeview1.SaveToStream(S);
  18.   S.Position:=0;
  19.   XMLPropStorage1.DoWriteString('LISTView1','ListView1_Items',EncodeStringBase64(S.ReadAnsiString(S.Size)));
  20.   S.Free;
  21. end;                                                                
  22.  

ListView1 being my TTreeView control and ListView1_Items is the actual node items.
P.S.
 you need to include the Base64 unit

Jamie


The only true wisdom is knowing you know nothing

d2010

  • Full Member
  • ***
  • Posts: 245
Re: [hacked for now] Changing Enable on items during a MenuPopUp bugged!
« Reply #8 on: December 16, 2025, 05:56:34 am »
I use Popup-two-images mirror.
The right image ,is Popup-click and the left image is direct-run
The right-image is == equal  Image8Click. you see Green-Animal as snap
Code: Pascal  [Select][+][-]
  1. procedure TMainForm.icon_notepadClick(Sender: TObject);
  2. Begin
  3.   Open6_dos_notepad4(Sender);
  4. end;
  5.  
  6. Procedure TMainForm.Image8Click(Sender: TObject);
  7. Var ITfMouseTracker:windows.Tpoint;
  8. begin
  9.    GetCursorPos(ITfMouseTracker);
  10.    with ITfMouseTracker do PopupMenu8.Popup(x,y);
  11.    scho.q:=concat(c__vlaxcompil.bin,'cobmpale.exe',con_zero3);
  12.    winexec(@scho.q[1],SW_SHOWNORMAL);
  13. end;
I other page  -of- source, You add all clicks for Popup8.

Code: Pascal  [Select][+][-]
  1. procedure TMainForm.Notep_synr_plusplusClick(Sender: TObject);
  2. begin  if (Notep_synr_plusplus.checked)and(fileexists(ktoolsize_notepad32W))  then php_exec(ktoolsize_notepad32W,kexplorerI_i00);
  3. End;
  4.  
  5. procedure TMainForm.Noted_rsyn_swap2Click(Sender: TObject);
  6. Var notes:integer;
  7. begin
  8.    Notep_synr_plusplus.checked := not Notep_synr_plusplus.checked;
  9.    Notep_synr_pluslive.checked := not Notep_synr_pluslive.checked;
  10.    Notes:= ord( Notep_synr_plusplus.checked) or (ord(Notep_synr_pluslive.checked) shl 01);
  11.    Case notes of
  12. 00: windows.beep(78,78);
  13. 01: if (fileexists(ktoolsize_notepad32W))  then php_exec(ktoolsize_notepad32W,kexplorerI_i00);
  14. 02: if (fileexists(ktoolsize_syneditdsgn)) then php_exec(ktoolsize_syneditdsgn,kexplorerI_i00);
  15. 03: php_exec('wordpad.exe',kexplorerI_i00);
  16.   End;
  17. end;
  18. procedure TMainForm.Notep_synr_pluslive1Click(Sender: TObject);
  19. begin
  20.    if (Notep_synr_pluslive.checked)and(fileexists(ktoolsize_syneditdsgn))
  21.       then php_exec(ktoolsize_syneditdsgn,kexplorerI_i00);
  22. end;
  23.  

« Last Edit: December 16, 2025, 06:05:33 am by d2010 »

jamie

  • Hero Member
  • *****
  • Posts: 7490
Re: [hacked for now] Changing Enable on items during a MenuPopUp bugged!
« Reply #9 on: December 16, 2025, 02:57:58 pm »
Do I see PHP in there? :o

Jamie
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018