Recent

Author Topic: Multiple Popup menus  (Read 10025 times)

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: Multiple Popup menus
« Reply #15 on: July 23, 2015, 04:24:32 pm »
You missed one :P

I have just downloaded the attached project and I'll post it back with any corrections.
Already corrected, See edit ...

@trayres:
Why did you go the length for a New Class ?
Just insert a(n empty) PopUpMenu and in the OnPopUp-Event do:
Code: [Select]
procedure <TYourForm>.PopupMenu1Popup(Sender: TObject);

var
  Item: TMenuItem;
  i: Integer;
  pp:TPopupMenu;  // <-- here

begin
  pp := TPopupMenu(Sender); // <-- here
  pp.Items.Clear;  // <-- Maybe, just to be sure
  for i := 0 to 5 do
  begin
    Item := TMenuItem.Create(pp); // <--- here
    Item.Caption := 'Item ' + IntToStr(i)+' (' + IntToStr(random(9))+')';
    item.tag := i;
    Item.OnClick := @PopupClicked;
    pp.Items.Add(Item); // <-- here
  end;
//  pp.PopUp; // <-- here
end;
The IntToStr(random(9) is just to demonstrate, it's generated new each time.
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Multiple Popup menus
« Reply #16 on: July 23, 2015, 04:39:08 pm »
OK I've attached the same project with all the hairy pieces removed and a few light comments. For any farther comments ask.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: Multiple Popup menus
« Reply #17 on: July 23, 2015, 04:51:29 pm »
OK I've attached the same project with all the hairy pieces removed and a few light comments. For any farther comments ask.
Hi, i think now you missed one ...
I think this is better:
Code: [Select]
procedure TForm1.HandleCommand;
begin
  case Edit1.Text of
    'Dynamic' : begin
       if Assigned(FDynamicPopup) then FreeAndNil(FDynamicPopup);// this is must otherwise you have a memmory leak
       FDynamicPopup := TDynamicPopup.Create(Self); // <-- Here
       FDynamicPopup.FillItems;
    end;
  else begin
      ShowMessage('Invalid Command');// Showmessage should be enough.
      Edit1.Text:='';//<-- bad choice! what happens if some one has typed Dynamik instead of Dynamic why force him to retype everything?
    end;
  end;
end;
This way it's also freed when the form closes.

[Edit]
Not Quite,
Better do a
Code: [Select]
       if Assigned(FDynamicPopup) then FreeAndNil(FDynamicPopup);// this is must otherwise you have a memmory leak
in FormDestroy
« Last Edit: July 23, 2015, 04:54:50 pm by jc99 »
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Multiple Popup menus
« Reply #18 on: July 23, 2015, 06:59:35 pm »
OK I've attached the same project with all the hairy pieces removed and a few light comments. For any farther comments ask.
Hi, i think now you missed one ...
I think this is better:
Code: [Select]
procedure TForm1.HandleCommand;
begin
  case Edit1.Text of
    'Dynamic' : begin
       if Assigned(FDynamicPopup) then FreeAndNil(FDynamicPopup);// this is must otherwise you have a memmory leak
       FDynamicPopup := TDynamicPopup.Create(Self); // <-- Here
       FDynamicPopup.FillItems;
    end;
  else begin
      ShowMessage('Invalid Command');// Showmessage should be enough.
      Edit1.Text:='';//<-- bad choice! what happens if some one has typed Dynamik instead of Dynamic why force him to retype everything?
    end;
  end;
end;
This way it's also freed when the form closes.
True! by assigning the owner in the TDynamicPopup constructor you instruct the owner to destroy the control when it destroys it self.

[Edit]
Not Quite,
Better do a
Code: [Select]
       if Assigned(FDynamicPopup) then FreeAndNil(FDynamicPopup);// this is must otherwise you have a memmory leak
in FormDestroy
No don't. Either set the owner in the constructor aka your previous comment in the quoted post or add the check on the destructor not both.

It is not problematic per see. To make things clear there will be no problem if you do both except a small waste of cpu cycles. look at it like this
1) you call TDynamicPopup.Create(aComponent); this means that TDynamicpop adds it self on the acomponent destroylist
2) acomponent in order to protect it self from GPFs or as known in fpc sigsegv adds it self in the dynamicpopup freenotification list

each time the tdynamicpopup is freed it walked down all the components in its freenotification list and notifies them about the imminent destruction of the TDynamicPopup. In this process the Owner gets a notification that a control is about to be destroyed and searches if that control is in its own lists if it is found it is removed moving the list items up the ladder as needed. And here you have a small conflict is the component destroyed by the owner or it has been destroyed by something else? If you add code to destroy it on the destructor you are circumventing the ownership mechanism and the search and remove is executed if you do not destroy it your self and let the owner mechanism do its job this is avoided.

The rest of the details are left as a small exercise for the reader. What is the optimum choice for this situation add self in the owner or add the freeandnil call in the destructor and why? What needs to be changed to make the other choice optimum?
« Last Edit: July 23, 2015, 07:01:52 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: Multiple Popup menus
« Reply #19 on: July 23, 2015, 09:33:56 pm »
No don't. Either set the owner in the constructor aka your previous comment in the quoted post or add the check on the destructor not both.
Yeah, it was meant either this or the other way.

The first way I've seen a possible problem, with create(Aowner) you add it to the list of components of the Owner/here the form.
Is it automatically removed from that list if you freeandnil it ? If yes then both ways are valid.
[edit]
Sorry you already answered that. So both ways are valid !
« Last Edit: July 23, 2015, 09:36:51 pm by jc99 »
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

trayres

  • Jr. Member
  • **
  • Posts: 92
Re: Multiple Popup menus
« Reply #20 on: July 24, 2015, 07:10:12 am »
I tried to expand this a little bit, to send the OnClick procedure the Caption string of the Item that was clicked, but I'm having trouble passing parameters.

Any thoughts on this part? Then the dynamic popup menu will truly be 'done' (For now...  :D)

[Edit] Ok I looked at the class for TMenuItem, and there's a PROPERTY for caption, but I can't seem to access it through the Sender : TObject - there must be a way to do this, I just don't know...

Btw, looking at the source code is A) really impressive and B) a kick ass way to learn. I still can't believe this is all free - it's just amazing.
« Last Edit: July 24, 2015, 07:15:13 am by trayres »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Multiple Popup menus
« Reply #21 on: July 24, 2015, 07:20:35 am »
I tried to expand this a little bit, to send the OnClick procedure the Caption string of the Item that was clicked, but I'm having trouble passing parameters.

Any thoughts on this part? Then the dynamic popup menu will truly be 'done' (For now...  :D )

[Edit] Ok I looked at the class for TMenuItem, and there's a PROPERTY for caption, but I can't seem to access it through the Sender : TObject - there must be a way to do this, I just don't know...

Btw, looking at the source code is A) really impressive and B) a kick ass way to learn. I still can't believe this is all free - it's just amazing.
Did you look at the code I posted? Closely? if not look closer your request has already been implemented there.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

trayres

  • Jr. Member
  • **
  • Posts: 92
Re: Multiple Popup menus
« Reply #22 on: July 25, 2015, 02:52:50 am »
Did you look at the code I posted? Closely? if not look closer your request has already been implemented there.

I apologize! I did not see that - you cast the sender in the OnClicked procedure - excellent, this is the missing piece I needed; thank you very much!

 

TinyPortal © 2005-2018