[SOLVED] Copy the contents of a popup menu into a submenu of another popup menu lazarus / 31-LCL ---------------------------------------------------------------------- fatmonk · 2021-03-02 11:55 I have two popup menus defined: pumMain and pumPresets. These are accessed by clicking two different buttions in my application. I would like to make the contents of pumPresets available as a submenu of pumMain without duplicating the menu. I see that TMenu.Assign and TMenuItem.Assign can be used to to copy menus and items, but is there a way to 'embed' pumPresets as a submenu of pumMain? At the moment I have just added an OnClick to the 'Presets' option in the main menu which opens the pumPresets popup menu. This works, but the 'Presets' option in the main menu doesn;t have the triangel to the right and the alignment of the Presets menu isn;t the same as if it were a true submenu... Any ideas? -FM ---------------------------------------------------------------------- jamie · 2021-03-02 21:46 The menus have an Owner draw option.. You can draw the little arrow if you wish during the drawing on the menu item.. ---------------------------------------------------------------------- fatmonk · 2021-03-03 13:14 That would solve the missing arrow issue, but the alignment and the fact that the previous menu closes when opening the second popup looks odd... maybe I need to rethink this whole approach. -FM ---------------------------------------------------------------------- lucamar · 2021-03-03 20:38 Any reason why you can't just assign the base menuitem of the popup as the direct child of the mainmenu item you want? Or traversing the popup items and assigning/adding to an item in the main menu submenu? If you don't know what I'm talking about, tell me and I'll try to make a small example tomorrow (it's a little late here-now). ---------------------------------------------------------------------- howardpc · 2021-03-04 09:27 You could try this. In a new Lazarus project, double-click the empty form to generate an OnCreate handler. Then copy the following code from the "private" declaration onwards, and run it. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit uMain; {$mode objfpc}{$H+} interface uses  Classes, Forms, Controls, Dialogs, Menus, StdCtrls; type  TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);  private    pumMain: TPopupMenu;    pumPresets: TPopupMenu;    mainButton: TButton;    presetButton: TButton;    submenuButton: TButton;    procedure MainButtonClick(Sender: TObject);    procedure PresetButtonClick(Sender: TObject);    procedure PresetOnClick(Sender: TObject);    procedure SubmenuButtonClick(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} procedure TForm1.FormCreate(Sender: TObject);begin  Caption := 'Menu copying example';   pumMain := TPopupMenu.Create(Self);  pumMain.Items.Add(NewItem('Main 1', 0, False, True, Nil, 0, ''));  pumMain.Items.Add(NewItem('Main 2', 0, False, True, Nil, 0, ''));  pumMain.Items.Add(NewItem('Main 3', 0, False, True, Nil, 0, ''));   pumPresets := TPopupMenu.Create(Self);  pumPresets.Items.Add(NewItem('Presets 1', 0, False, True, @PresetOnClick, 0, ''));  pumPresets.Items.Add(NewItem('Presets 2', 0, False, True, @PresetOnClick, 0, ''));  pumPresets.Items.Add(NewItem('Presets 3', 0, False, True, @PresetOnClick, 0, ''));   mainButton := TButton.Create(Self);  with mainButton do    begin      AutoSize := True;      Caption := 'Popup main';      OnClick := @MainButtonClick;      BorderSpacing.Around := 20;      AnchorSideTop.Control := Self;      AnchorSideLeft.Control := Self;      Parent := Self;    end;   presetButton := TButton.Create(Self);  with presetButton do    begin      AutoSize := True;      Caption := 'Popup preset';      OnClick := @PresetButtonClick;      BorderSpacing.Around := 20;      AnchorSideLeft.Control := Self;      AnchorSideTop.Control := mainButton;      AnchorSideTop.Side := asrBottom;      Parent := Self;    end;   submenuButton := TButton.Create(Self);  with submenuButton do    begin      AutoSize := True;      Caption := 'Add preset to main';      OnClick := @SubmenuButtonClick;      BorderSpacing.Around := 20;      AnchorSideLeft.Control := Self;      AnchorSideTop.Control := presetButton;      AnchorSideTop.Side := asrBottom;      Parent := Self;    end;end; procedure TForm1.MainButtonClick(Sender: TObject);begin  pumMain.PopUp;end; procedure TForm1.PresetButtonClick(Sender: TObject);begin  pumPresets.PopUp;end; procedure TForm1.PresetOnClick(Sender: TObject);begin  if Sender is TMenuItem then    ShowMessage('You clicked menu captioned '+TMenuItem(Sender).Caption);end; procedure TForm1.SubmenuButtonClick(Sender: TObject);var  mi, smi: TMenuItem;  i: Integer;begin  mi := TMenuItem.Create(pumMain);  pumMain.Items[2].Insert(0, mi);  mi.Caption := 'Added Presets';  for i := 0 to pumPresets.Items.Count-1 do    begin      smi := TMenuItem.Create(pumMain);      smi.Caption := pumPresets.Items[i].Caption;      smi.OnClick := pumPresets.Items[i].OnClick;      mi.Add(smi);    end;  submenuButton.Enabled := False;end; end. ---------------------------------------------------------------------- fatmonk · 2021-03-11 15:39 Quote from: lucamar on March 03, 2021, 09:38:50 pmassign the base menuitem of the popup as the direct child of the mainmenu item you want Thats sounds like exactlywhat I want to do (except that both are actually popup menus rather than one being a MainMenu), but I have no idea how to do that. I was going to knock up a demo project but haven't had a chance yet.. if you can give any pointers that woudl be useful, otherwise I'll put that demo together and maybe you can point out how to do it with that. Thanks, -FM ---------------------------------------------------------------------- lucamar · 2021-03-11 16:04 All menus, whether PopUp of MainMenu, have a property Items which is itself a TMenuItem, so suppossing you have an item in some menu called, say. SubItem and a popup called EditPopUp, you just have to do: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SubItem.Items.Assign(EditPopUp.Items);Note that I've not tested it but it should work. You might have to change some properties, perhaps, but that will be evident after the initial testing. HTH! ---------------------------------------------------------------------- fatmonk · 2021-03-18 12:11 Hi Lucamar, Thank for the pointer.. after a bit of playing around I figured it out. The correct syntax is: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   miPresets.Assign(pumPresets.Items); where miPresets is the appropriate TMenuItem in the main TPopupMenu and pumPresets is the second TPopUpMenu that I want to open when the menu item in the main popup menu is clicked. The Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i assign, however, removes the existing Caption for the MenuItem, so I also needed to re-assign that with: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   miPresets.Caption:='Presets';   All good now, though  -FM ---------------------------------------------------------------------- lucamar · 2021-03-18 13:38 Yeah, sorry; I made an error because I used a top menu item in a main menu for the tests Though it should have worked also for a pop up, leaving the "parent" item intact ... looks like I've got to test more deeply how these things work