PDA

View Full Version : FUNCTION: Adjust Menu Item Properties


Desmond
10-04-2005, 11:00 AM
Hello,

This function allows you to change any individual menu item's properties at runtime using only it's ID. Please note that menu items with sub-items have an ID of -1, and therefore will not work with this function.

-- NAME: AdjustSubMenuProperties
-- VALUES: tSubMenu: A numerically indexed menu (or sub-menu table)
-- tItemProperties: A table of properties (Enabled, Checked, ID, Text, SubMenu)
-- RETURNS: (table) The complete (adjusted) menu structure
-- TO USE: Pass this function a table containing the project's menu (from Application.GetMenu)
-- and a table containing item properties. The item properties table MUST contain the ID
-- or this function will fail.


function AdjustSubMenuProperties(tSubMenu, tItemProperties)
for nIndex, tItem in tSubMenu do
if tItem.ID == tItemProperties.ID then
-- The ID's match, this is the item to change
-- We need to fill in any non-included properties (so they're not changed)
for sKey, vValue in tItem do
if tItemProperties[sKey] == nil then
-- A property existing in the menu was not passed to this function.
-- assign the current value to the new property table
tItemProperties[sKey] = vValue;
end
end

-- Assign the new properties over the old ones
tSubMenu[nIndex] = tItemProperties;

-- Break out of the loop, we're done.
break;
elseif type(tItem.SubMenu) == 'table' and tItem.ID == -1 then
-- The ID didn't match, and a sub-menu exists. Recurse.
tSubMenu[nIndex].SubMenu = AdjustSubMenuProperties(tSubMenu[nIndex].SubMenu, tItemProperties);
end
end

-- Return.
return tSubMenu;
end

Example Function Call:
-- Get the current menu structure
tMenu = Application.GetMenu();

-- Adjust menu structure to disable item with ID 301 (and change it's text to 'Desmond')
tMenu = AdjustSubMenuProperties(tMenu, {ID=301, Text = "Desmond", Enabled = false});

-- Apply the new menu structure to the application
Application.SetMenu(tMenu);

This function could definitely use some error-checking, but it should at least serve to get you started! Enjoy!

AXXESS
10-04-2005, 11:56 AM
Very cool, Desmond.... just what I was looking for! :yes :yes :yes :)

rhosk
10-07-2005, 12:30 PM
Desmond, would this work if I just wanted to add a checkmark to an existing ID?


The item properties table MUST contain the ID or this function will fail.

I'm guessing "no" with that statement. What I would like to do is execute a menu item and add a checkmark next to the existing item, which of course, would need a different ID for any subsequent logic. I mean, this is perfect if you only have enabled/disabled items. But, if you can't merely change the ID (or is there a way?...barring a runtime menu), then I don't see the advantage to this.

PS, a nice solution would be to allow "hidden" menu items and call them when you need them. I tried to just assign an ID with blank text and that's exactly what I got for an output, lol.

rhosk
10-07-2005, 01:10 PM
Disregard, figured it out :D

Desmond
10-07-2005, 01:24 PM
Hey Rhosk,

Glad you got it working!

Were you able to use the above to add the checkbox? It *should* work. :)

Let me know!

rhosk
10-07-2005, 01:33 PM
Were you able to use the above to add the checkbox? It *should* work.

Actually, it doesn't, because the ID has to exist. I just callled the table involved and changed it that way. Easy enough for what little I want to do. Nice function, though :yes

Desmond
10-07-2005, 02:03 PM
Oh, I think I understand.

You could wrap this into your own function like this:

function ItemSetChecked(nID, bChecked)
-- Get the current menu structure
tMenu = Application.GetMenu();

-- Adjust menu structure to disable item with ID 301 (and change it's text to 'Desmond')
tMenu = AdjustSubMenuProperties(tMenu, {ID=nID, Checked=bChecked});

-- Apply the new menu structure to the application
Application.SetMenu(tMenu);
end

And then call it:
ItemSetChecked(101, true);

The original function I wrote compares the ID you pass in the table to each ID in the table, and applies the properties table only if the ID's match. That's why it's required. If that makes any sense. :)

Glad you found my function useful!

rhosk
10-07-2005, 02:31 PM
Nice wrap!! Thanks for your help, I appreciate it. Have a great weekend!

Desmond
10-07-2005, 02:45 PM
Sure thing. You're most welcome.

Happy turkey day!

Roboblue
02-26-2006, 11:07 AM
I have seen some great menu bar examples. But one thing I need seems to be missing.
I need to have, from page to page, menus that come and go. NOT enabled/disabled.
For example, on a 10 page project, 8 of them have several objects that need to be edited. On page 2, there would be 4 edit calls from the EDIT menu. On page 4, there may only be two edit calls. I can change the text and enable/disable the sub menus, but, because I can't "hide" the other two submenus, then page 4 will have two disabled (greyed) entries and two enabled entries. I don't think that is a very professional look.
To get around this, I have created scripts to run on each page (menu bar disabled) that create the menu on that page. This "mostly" works. The biggest con is that there has to be a script for each page, and in my project, two scripts for each page as I have different menus for the demo version, then for the registered version. I have a total of 42 scripts. Even the most minor of changes requires all of them to be edited. I have a search and replace app I built that will make the changes in all the scripts at once, but it's a lot of maintenance. and testing.
So, if there IS a way to remove/add menus (created with the Menu Bar enabled) at page runtime, I would appreciate the sharing of that information.

Desmond
02-27-2006, 09:13 AM
Well, I suppose you could store your menu structure in a table (or multiple tables, say one table for each sub-menu -- one for File, one for Edit) . . . then combine whatever arrays you'll use on the current page, and use an Application.SetMenu action to create the menu.

Stir and repeat for every page.

Roboblue
02-27-2006, 10:23 AM
Well, I suppose you could store your menu structure in a table (or multiple tables, say one table for each sub-menu -- one for File, one for Edit) . . . then combine whatever arrays you'll use on the current page, and use an Application.SetMenu action to create the menu.

Stir and repeat for every page.

Can we get a small example of one menu with a couple of submenu's?
I am still not real quick with the tables.
And from that, could that table be put into SQLite?
Also, will the menu bar have to be enabled with this method?

usernameCasper
11-15-2006, 04:57 AM
hey folks,
im starting to use this software couple days ago.
i know some stuff about vbscripting and this is kind the way this software acts the same =).
I did some tests with the enable/disable menubaritems, and i can put the text (strings) into a paragraph or textfield, or whatever via setProperties, but cant see a good way to enable / disable items when clicked on a button.
While it can be done with setting different Text, it should also be done with set with booleans.
I wonder how to do this, i observed some posts and coded for some hours, but i think it will only have to do with arrays? Dont the items in menubar, such as "File" with "ID=100", can be manipulate with some event on click like for instance a button?
So it would act like this: when click on button -->
menuInfo = Application.getProperties();
menuInfo1 = menuInfo.Enabled = true;

And no, i dont want to set each item apart from scripting them, as it seems there is a option in the GUI, which scripting is already done when putting those new items in.

Im at work, so i don't know the code exaclty what worked with the text, since i cant use a guide in the program itself. I will post it when im home.

So where are those itemsProperties as it only can put in tables ?
there must be something to handle properties from a single ID.
For example: e_ID = 100 ==> event from ID 100
why not:
menuInfo = Application.getProperties()
if menuInfo.ID(100) then -- OR if menuInfo(menuInfo.ID = 100) then
Application.setProperties(menuInfo.Enable = true);
end

Like in other program languages this isnt a hard deal ...
Plsz some clearyfication.
Kind regards,
Casper

usernameCasper
11-15-2006, 04:32 PM
Found it out :D :D :

On Preload:


tblMenu = Application.GetMenu();
tblMenu[1].Enabled = false;
tblMenu[2].Enabled = false;
Application.SetMenu(tblMenu);


Greets,
Casper

Roboblue
11-15-2006, 06:06 PM
hey, much better than what i've been doing.
Thanx!

usernameCasper
11-16-2006, 02:11 AM
you're welcome :)
If you have any questions, u can post them and i will have a look.

1 question to staff, is it possible to set items in menubar visible = true / false?
Im gonna work on *.ini-files for multiple languages in menubar today when im home, have a good day :)

Kind Regards,
Casper

TristanD
11-23-2006, 05:46 AM
Casper, Could you explain how you work with INI files to change languages ??

Just Curious

usernameCasper
11-24-2006, 03:00 AM
Well, im coding this is in C++, not in AMS.
You make a language.ini file or language.dll.
For example, you replace English Textobjects with Dutch Textobjects and give a value in that file. Language = 1 or Language = 2.
If Language == 1 then ...
Etc.

Sergio_S
10-02-2007, 11:50 AM
In the beginning of this old thread, Desmond has posted a very very useful example of function to change menu items on runtime, for menus created through Project > Menu Bar dialog.

Nevertheless, the function does not work for items with submenus (-1)a nd I need it.

1. How do I can reach that? I tried make some changes in that function but I could not get it.

2. How to do that also for a multi level menus, with sub sub menu items?

Is there someone that can help me in this job?