Recent

Author Topic: Multiple Popup menus  (Read 10024 times)

trayres

  • Jr. Member
  • **
  • Posts: 92
Multiple Popup menus
« on: July 19, 2015, 09:21:52 pm »
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

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: Multiple Popup menus
« Reply #1 on: July 19, 2015, 10:12:32 pm »
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!
I normally use one popup-menu that is dynamically changed, according to what I need.
   ... but let me have a try ...
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

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: Multiple Popup menus
« Reply #2 on: July 19, 2015, 10:31:46 pm »
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.
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

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Multiple Popup menus
« Reply #3 on: July 20, 2015, 12:38:15 am »
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

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: Multiple Popup menus
« Reply #4 on: July 20, 2015, 02:11:36 am »
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.
This is really a good example for a fully dynamic popup-menu generation and selection.
Now trayres has a good base to start on.
« Last Edit: July 20, 2015, 02:18:45 am 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

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: Multiple Popup menus
« Reply #5 on: July 20, 2015, 02:49:09 am »
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.
 
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 #6 on: July 23, 2015, 03:31:31 am »
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

  • Hero Member
  • *****
  • Posts: 5368
Re: Multiple Popup menus
« Reply #7 on: July 23, 2015, 04:48:08 am »
Later I call the code:
Code: [Select]
DynamicPopup.Create(Nil); <- Segfaults

Code: [Select]
TDynamicPopup.Create(Nil); //<- the class is named TDynamicPopup not DynamicPopup
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 #8 on: July 23, 2015, 06:19:58 am »
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!?
« Last Edit: July 23, 2015, 06:37:05 am by trayres »

trayres

  • Jr. Member
  • **
  • Posts: 92
Re: Multiple Popup menus
« Reply #9 on: July 23, 2015, 07:08:52 am »
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.  :D

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Multiple Popup menus
« Reply #10 on: July 23, 2015, 02:01:51 pm »
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.  :D
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.
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 #11 on: July 23, 2015, 03:58:10 pm »
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

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: Multiple Popup menus
« Reply #12 on: July 23, 2015, 04:03:12 pm »
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.
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 ...
« Last Edit: July 23, 2015, 04:12:00 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 #13 on: July 23, 2015, 04:10:03 pm »
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.
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!  :D

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Multiple Popup menus
« Reply #14 on: July 23, 2015, 04:15:16 pm »
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.
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.
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;   
You missed one :P

I have just downloaded the attached project and I'll post it back with any corrections.
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

 

TinyPortal © 2005-2018