Multiple Popup menus lazarus / 31-LCL ---------------------------------------------------------------------- trayres · 2015-07-19 19:21 I have a TPopup configured for my main form, but how do I dynamically select one of several popup menus, depending on a variable or on what was right-clicked on? I'm sure somebody has done it before, but I didn't find anything on the forum for "Multiple popup menus", so here we are! Thanks all! ---------------------------------------------------------------------- jc99 · 2015-07-19 20:12 Quote from: trayres on July 19, 2015, 09:21:52 pmI have a TPopup configured for my main form, but how do I dynamically select one of several popup menus, depending on a variable or on what was right- clicked on? I'm sure somebody has done it before, but I didn't find anything on the forum for "Multiple popup menus", so here we are! Thanks all! I normally use one popup-menu that is dynamically changed, according to what I need.    ... but let me have a try ... ---------------------------------------------------------------------- jc99 · 2015-07-19 20:31 Hi, i made a little test project, with two popup-Menus, and two panels , and the popup-menu is automatically selected on what panel you click by setting the panel.popup-property I hope it helps, otherwise tell me more what went wrong. Post some code, maybe a test-project. ---------------------------------------------------------------------- howardpc · 2015-07-19 22:38 The attached project shows one way to choose between dynamically constructed popup menus. Clicking the selected popup menu when shown executes a dynamically assigned OnClick handler. For simplicity the demonstration popups are attached to the container form, but you could adapt this scheme to show the selected popup on any TControl that the form contained. ---------------------------------------------------------------------- jc99 · 2015-07-20 00:11 Quote from: howardpc on July 20, 2015, 12:38:15 amThe attached project shows one way to choose between dynamically constructed popup menus. Clicking the selected popup menu when shown executes a dynamically assigned OnClick handler. For simplicity the demonstration popups are attached to the container form, but you could adapt this scheme to show the selected popup on any TControl that the form contained. This is really a good example for a fully dynamic popup-menu generation and selection. Now trayres has a good base to start on. ---------------------------------------------------------------------- jc99 · 2015-07-20 00:49 One addition: When using this template and adding Controls, the OnMouseDown-Event must be set on all Controls with dynamic popup. The benefit is that you get the clicked-control in the sender- Parameter. ---------------------------------------------------------------------- trayres · 2015-07-23 01:31 Code: [Select]    { TDynamicPopup }   TDynamicPopup = class(TPopupMenu) {    TMenuItem}     constructor Create(AOwner: TComponent); override;     class procedure PopupClicked(Sender: TObject);   end;          Code: [Select]constructor TDynamicPopup.Create(AOwner: TComponent); var   Item: TMenuItem;   i: Integer; begin inherited  Create(AOwner); //TPopupMenu.Create(Nil); for i := 0 to 5 do   begin     Item := TMenuItem.Create(DynamicPopup);     Item.Caption := 'Item ' + IntToStr(i);     Item.OnClick := @PopupClicked;     DynamicPopup.Items.Add(Item);   end; DynamicPopup.PopUp; end;    Later I call the code: Code: [Select] DynamicPopup.Create(Nil); <- Segfaults I'm not quite sure why I'm getting a segfault (yet) - all the code thus far has really helped, I have other popup menus working perfectly, but this one (where I build the menu items based on input) is being a bit of a bear.... Any thoughts? Thanks again for everyone's help. This is really a great forum! ---------------------------------------------------------------------- taazz · 2015-07-23 02:48 Quote from: trayres on July 23, 2015, 03:31:31 amLater I call the code: Code: [Select] DynamicPopup.Create(Nil); <- Segfaults Code: [Select] TDynamicPopup.Create(Nil); //<- the class is named TDynamicPopup not DynamicPopup ---------------------------------------------------------------------- trayres · 2015-07-23 04:19 Still segfaults; I have a DynamicPopup : TDynamicPopup declared in the VAR of my interface; Now I get the segfault in this: Code: [Select]DynamicPopup.Items.Add(Item); [Edit] DynamicPopup is not declared yet, I think! So... what the hell did I do wrong!? ---------------------------------------------------------------------- trayres · 2015-07-23 05:08 I figured it out! Code: [Select]constructor TDynamicPopup.Create(AOwner: TComponent); begin inherited  Create(AOwner); //inherited  Create(AOwner); //TPopupMenu.Create(Nil); end; Code: [Select]procedure TDynamicPopup.FillItems; var   Item: TMenuItem;   i: Integer;   begin   for i := 0 to 5 do   begin     Item := TMenuItem.Create(DynamicPopup);     Item.Caption := 'Item ' + IntToStr(i);     Item.OnClick := @PopupClicked;     DynamicPopup.Items.Add(Item);   end; DynamicPopup.PopUp;   end;  I separated the constructor (which just calls the superclass' constructor) from the code to implement the fill - that way, the object that it tries to add TMenuItems to exists *before* they are created. Seems to be working a charm - I might have a memory leak, but I'm unsure. Thanks for your help everyone! This is all brilliant. ---------------------------------------------------------------------- taazz · 2015-07-23 12:01 Quote from: trayres on July 23, 2015, 07:08:52 amI figured it out! Code: [Select]constructor TDynamicPopup.Create(AOwner: TComponent); begin inherited  Create(AOwner); //inherited  Create(AOwner); //TPopupMenu.Create(Nil); end; Code: [Select]procedure TDynamicPopup.FillItems; var   Item: TMenuItem;   i: Integer;   begin   for i := 0 to 5 do   begin     Item := TMenuItem.Create(DynamicPopup);     Item.Caption := 'Item ' + IntToStr(i);     Item.OnClick := @PopupClicked;     DynamicPopup.Items.Add(Item);   end; DynamicPopup.PopUp;   end;  I separated the constructor (which just calls the superclass' constructor) from the code to implement the fill - that way, the object that it tries to add TMenuItems to exists *before* they are created. Seems to be working a charm - I might have a memory leak, but I'm unsure. Thanks for your help everyone! This is all brilliant.  Scary code. Really scary code. Create a minimal application with the problem or your working code and attach it to a post here. What you have shown us does not look very stable. ---------------------------------------------------------------------- trayres · 2015-07-23 13:58 Attached is a demonstration project with the TDynamicPopup and how I call/use it - I separate the constructor call from the fill call, and bind the OnClick to a procedure (that will eventually be able to take the item clicked's Value as a parameter - I am using this as a context click menu in my EDA application, which is still very primitive). Any and all comments welcome; thanks for taking the time to look. After I make more progress I'll end up posting what code I have, it's just... a little embarrassing right now, compared to ZCAD and some of the great stuff I've seen. I am still working on movable "Schematic Objects" and doing coordinate transformations, etc. Anyway, let me know what everyone things of the way I'm doing this "dynamic popup"; to use it, type "Dynamic" in the Edit1 text box, and press enter. It has no dependencies outside of stock FPC/Lazarus. Thanks! ---------------------------------------------------------------------- jc99 · 2015-07-23 14:03 Quote from: taazz on July 23, 2015, 02:01:51 pmScary code. Really scary code. Create a minimal application with the problem or your working code and attach it to a post here. What you have shown us does not look very stable. Hi taazz, it's me again ... In this one i fully agree, but it would be helpfull, to say, what is the scary thing: e.G: using an external Variable 'DynamicPopup' in Fillitems. Better do: Code: [Select]procedure TDynamicPopup.FillItems; // Maybe FillItemsAndPopup would be a better name var   Item: TMenuItem;   i: Integer; begin   Items.Clear;  // <-- Maybe, just to be sure   for i := 0 to 5 do   begin     Item := TMenuItem.Create(Self); // <--- here     Item.Caption := 'Item ' + IntToStr(i);     Item.OnClick := @PopupClicked;     Self.Items.Add(Item); // <--- here   end;   Self.PopUp; // <-- here end;  [Edit] OOps, i forgot one ... ---------------------------------------------------------------------- trayres · 2015-07-23 14:10 Quote from: jc99 on July 23, 2015, 04:03:12 pmQuote from: taazz on July 23, 2015, 02:01:51 pmScary code. Really scary code. Create a minimal application with the problem or your working code and attach it to a post here. What you have shown us does not look very stable. Hi taazz, it's me again ... In this one i fully agree, but it would be helpfull, to say, what is the scary thing: e.G: using an external Variable 'DynamicPopup' in Fillitems. Better do: Code: [Select]procedure TDynamicPopup.FillItems; // Maybe FillItemsAndPopup would be a better name var   Item: TMenuItem;   i: Integer; begin   Items.Clear;  // <-- Maybe, just to be sure   for i := 0 to 5 do   begin     Item := TMenuItem.Create(Self); // <--- here     Item.Caption := 'Item ' + IntToStr(i);     Item.OnClick := @PopupClicked;     DynamicPopup.Items.Add(Item);   end;   Self.PopUp; // <-- here end;  Ah, yes, definitely!  I see the dependency. It is now improved. Thank you! ---------------------------------------------------------------------- taazz · 2015-07-23 14:15 Quote from: jc99 on July 23, 2015, 04:03:12 pmQuote from: taazz on July 23, 2015, 02:01:51 pmScary code. Really scary code. Create a minimal application with the problem or your working code and attach it to a post here. What you have shown us does not look very stable. Hi taazz, it's me again ... In this one i fully agree, but it would be helpfull, to say, what is the scary thing: well that is what I'm trying to do but I can't correct one thing breaking everything else it will look like stupid advice correcting the attached project commenting on the wrong parts is the only logical thing I can think of. Quote from: jc99 on July 23, 2015, 04:03:12 pme.G: using an external Variable 'DynamicPopup' in Fillitems. Better do: Code: [Select]procedure TDynamicPopup.FillItems; // Maybe FillItemsAndPopup would be a better name var   Item: TMenuItem;   i: Integer; begin   Items.Clear;  // <-- Maybe, just to be sure   for i := 0 to 5 do   begin     Item := TMenuItem.Create(Self); // <--- here     Item.Caption := 'Item ' + IntToStr(i);     Item.OnClick := @PopupClicked;     DynamicPopup.Items.Add(Item);   end;   Self.PopUp; // <-- here end;  You missed one I have just downloaded the attached project and I'll post it back with any corrections. ---------------------------------------------------------------------- jc99 · 2015-07-23 14:24 Quote from: taazz on July 23, 2015, 04:15:16 pmYou missed one 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 .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. ---------------------------------------------------------------------- taazz · 2015-07-23 14:39 OK I've attached the same project with all the hairy pieces removed and a few light comments. For any farther comments ask. ---------------------------------------------------------------------- jc99 · 2015-07-23 14:51 Quote from: taazz on July 23, 2015, 04:39:08 pmOK 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 ---------------------------------------------------------------------- taazz · 2015-07-23 16:59 Quote from: jc99 on July 23, 2015, 04:51:29 pmQuote from: taazz on July 23, 2015, 04:39:08 pmOK 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. Quote from: jc99 on July 23, 2015, 04:51:29 pm[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? ---------------------------------------------------------------------- jc99 · 2015-07-23 19:33 Quote from: taazz on July 23, 2015, 06:59:35 pmNo 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 ! ---------------------------------------------------------------------- trayres · 2015-07-24 05:10 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...  ) [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. ---------------------------------------------------------------------- taazz · 2015-07-24 05:20 Quote from: trayres on July 24, 2015, 07:10:12 amI 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...  ) [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. ---------------------------------------------------------------------- trayres · 2015-07-25 00:52 Quote from: taazz on July 24, 2015, 07:20:35 amDid 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!