Get Menu check status

Discussions related to Visual Prolog
daveplummermd
VIP Member
Posts: 88
Joined: 18 Jul 2006 17:18

Get Menu check status

Unread post by daveplummermd » 5 May 2018 9:34

Hello
I have 8.01 CE

How does one get the check value of a taskwindow menu item. I understand a workaround by creating a fact to keep track, but iI bet there is a more direct predicate.
If there is example file , just refer me there.

Thanks in advance

dave
Dave Plummer

Harrison Pratt
VIP Member
Posts: 304
Joined: 5 Nov 2000 0:01

Re: Get Menu check status

Unread post by Harrison Pratt » 6 May 2018 13:41

Dave,

This is how to expose what's going on in the menu structure:

Code: Select all

predicates     onTestChecktest : window::menuItemListener. clauses     onTestChecktest(Source, MenuTag) :-         M = vpi::menuGet(Source:getVpiWindow()),         stdio::write("\n", M),         vpi::menuCheck(Source:getVpiWindow(), "CheckTest", b_false), % set/unset check         M2 = vpi::menuGet(Source:getVpiWindow()),         stdio::write("\n", M2).
It appears that you must drill down through the menuItem*, extract txt(MenuTag,"CheckTest",noAccelerator,1,1,[]) and look at ItemState.

You can find the structure in vpiDomains.cl and the ItemState (0 or 1) is what you are looking for.

Code: Select all

    menuItem =         txt(menuTag Command, string Text, acceleratorKey Key, booleanInt Enabled, integer ItemState, menuItem* SubMenu);         ownerdraw(menuTag Command, integer Val, booleanInt Enabled, booleanInt Checked, menuItem* SubMenu);         separator;         menuBreak [explicitTag].     menuItem_list = menuItem*.
I wrote some code to pull that out many year ago, but can't find it. It ought to be a built-in predicate, but I can't find one that does what you need. Perhaps you could share it when you get it solved. :-)

Harrison Pratt
VIP Member
Posts: 304
Joined: 5 Nov 2000 0:01

Re: Get Menu check status

Unread post by Harrison Pratt » 6 May 2018 19:04

Here's an approach. I haven't fully fleshed it out, but it will get you started. Of course, there is room for improvement and I'm probably missing some built-in predicates that would make it simpler.

In a class declaration (.cl) file:

Code: Select all

predicates       getMenuItem : (vpiDomains::menu MenuItemList, vpiDomains::menuTag SeekTag) -> vpiDomains::menuItem.     % Raise exception if SeekTag is not in MenuItemList       getWindowMenuItem : (window, vpiDomains::menuTag) -> vpiDomains::menuItem.       toggleMenuCheck : (window, vpiDomains::menuTag).       getMenuCheckedState : (window, vpiDomains::menuTag) -> boolean.
In the implementation (.pro) file:

Code: Select all

    open core, vpiDomains   clauses     getMenuCheckedState(W, Tag) = CheckedState :-         txt(Tag, _Text, _Key, _Enabled, State, _MoreItems) = getWindowMenuItem(W, Tag),         !,         if State = b_false then             CheckedState = false % NOTE that this predicate is using two different boolean domains         else             CheckedState = true         end if.     getMenuCheckedState(_, Tag) = _ :-         Msg = string::format("Unable to retrieve MenuItem with tag = '%'", Tag),         exception::raise_user(Msg).       toggleMenuCheck(W, Tag) :-         if State = getMenuCheckedState(W, Tag) then             if State = false then                 NewState = b_true % NOTE that this predicate is using two different boolean domains             else                 NewState = b_false             end if,             vpi::menuCheck(W:getVpiWindow(), Tag, NewState)         else             exception::raise_user("Unable to get State from getMenuCheckedState(Win,Tag)")         end if.       getWindowMenuItem(W, SeekTag) = MenuItem :-         Menu = vpi::menuGet(W:getVpiWindow()),         MenuItem = getMenuItem(Menu, SeekTag).       getMenuItem(Menu, SeekTag) = MenuItem :-         Menu = dynMenu(MenuItemList),         MItemSubList in MenuItemList,         MenuItem = tryGetTaggedItem([MItemSubList], SeekTag),         !.     getMenuItem(_Menu, SeekTag) = _ :-         Msg = string::format("Unable to find tag '%' in menu", SeekTag),         exception::raise_user(Msg).   class predicates     tryGetTaggedItem : (menuItem_list, menuTag) -> menuItem determ. clauses     tryGetTaggedItem([Item | _T], SeekTag) = Item :-         Item = txt(SeekTag, _Text, _Key, _Enabled, _State, _MoreItems),         !.     tryGetTaggedItem([H | _T], SeekTag) = U :-         H = txt(_Tag, _Text, _Key, _Enabled, _State, MoreItems),         U = tryGetTaggedItem(MoreItems, SeekTag),         !.     tryGetTaggedItem([_H | T], SeekTag) = U :-         U = tryGetTaggedItem(T, SeekTag).

Some test code in the TaskWindow:

Code: Select all

predicates     onTestChecktest : window::menuItemListener. clauses     onTestChecktest(Source, MenuTag) :-         g_Menu::toggleMenuCheck(Source, MenuTag), % toggle the checked flag         Resp = g_Menu::getMenuCheckedState(Source, MenuTag), % get the new checked status         stdio::write("\n Checked state: ", toString(Resp)). % show the new state

Harrison Pratt
VIP Member
Posts: 304
Joined: 5 Nov 2000 0:01

Re: Get Menu check status

Unread post by Harrison Pratt » 7 May 2018 13:17

See attached files, cleaned up and better documented.
Apparently one can check/uncheck menu items even if "Checked" is not set in the IDE menu editor.
You should be able to read the Checked status from anywhere in your application, not just the menu event handlers.
Attachments
g_MenuKit.cl
(645 Bytes) Downloaded 105 times
g_MenuKit.pro
(2.34 KiB) Downloaded 113 times

daveplummermd
VIP Member
Posts: 88
Joined: 18 Jul 2006 17:18

Re: Get Menu check status

Unread post by daveplummermd » 16 Oct 2018 13:08

Thanks for this excellent reply!
Dave Plummer

daveplummermd
VIP Member
Posts: 88
Joined: 18 Jul 2006 17:18

Re: Get Menu check status

Unread post by daveplummermd » 16 Oct 2018 13:39

how do I get the vpiDomains::menuTag from the resourceidentifier defined in the IDE?

thanks in advance
dave
Dave Plummer

Harrison Pratt
VIP Member
Posts: 304
Joined: 5 Nov 2000 0:01

Re: Get Menu check status

Unread post by Harrison Pratt » 17 Oct 2018 11:56

First, create a menu item that you want to check. I don't think that you can check/uncheck a top level menu item, so it needs to be a sub-menu item. Setting or unsetting Checked in the IDE only changes the initial state of the menu item; you don't need to check this to make it a "checkable" item.

For the example below I created a menu item Log events in the File menu cascade. That menu item is labelled id_file_log_events in the resourceIdentifiers.i file. The IDE's code-completion will expand a resource identifier name for you if you start typing resourceIdentifiers::id_file...

Of course you can just copy/paste from the menu expert dialog, too.

The g_menu predicate is in the g_menuKit class posted earlier.

Code: Select all

    testMenuCheck() :-         if g_menu::getMenuItemCheck(applicationSession::getSessionWindow(),resourceIdentifiers::id_file_log_events) = true then             stdio::write("\nChecked")         else             stdio::write("\nunchecked")         end if.

daveplummermd
VIP Member
Posts: 88
Joined: 18 Jul 2006 17:18

Re: Get Menu check status

Unread post by daveplummermd » 24 Oct 2018 16:48

perfect!
Thanks
Dave Plummer

Post Reply