Page 1 of 1

Pull-Down Menu Stack For NoSave Standalone

Posted: Wed Apr 16, 2014 7:46 pm
by TerryL
Any self-respecting menu bar disables the Edit menuItems cut/copy/paste under certain conditions. I've been having two problems re-enabling them. Any help would be much appreciated. (Win 7, LC 6.5.2)

The LiveCode User Guide suggests the disable/enable code go in the grp "Menubar 1" script. "If you want to dynamically change a menu's contents with a mouseDown handler at the time the menu is displayed, you must place the mouseDown handler in the group's script. When a menu button is being displayed in the Mac OS menu bar, it does not receive mouseDown messages, but its group does."

So in grp "Menubar 1" script:

Code: Select all

on mouseDown  --enable/disable menuItems
   switch (the short name of target())
      case "Edit"
         get selectedChunk()
         if it = empty then  --no inserted cursor, no selected text
            disable menuItem 1 of menu "Edit"  --cut
            disable menuItem 2 of menu "Edit"  --copy
            disable menuItem 3 of menu "Edit"  --paste
         else if word 2 of it > word 4 of it then  --inserted cursor
            disable menuItem 1 of menu "Edit"  --cut
            disable menuItem 2 of menu "Edit"  --copy
            enable menuItem 3 of menu "Edit"  --paste
         else if word 2 of it < word 4 of it then  --selected text
            --disable menuItem 1 of menu "Edit"  --possible bug, won't enable without disable first if previously disabled
            enable menuItem 1 of menu "Edit"  --cut
            enable menuItem 2 of menu "Edit"  --copy
            enable menuItem 3 of menu "Edit"  --paste
         end if
         break
   end switch
end mouseDown
   
on mouseLeave  --enable menuItems, allow keyboard equivs
   if the short name of target() = "Edit" then
      --disable menuItem 1 of menu "Edit"  --possible bug, won't enable without disable first if previously disabled
      enable menuItem 1 of menu "Edit"  --cut
      enable menuItem 2 of menu "Edit"  --copy
      enable menuItem 3 of menu "Edit"  --paste
   end if
end mouseLeave
Problem1: There may be something wrong with the enable command. If I use the Edit menu to cut and paste text, immediately select new text, I can't use the Edit menu because cut/copy menuItems are still disabled.

After Edit menuItems are disabled, they fail to re-enable using the pull-down menu. This seems to be fixed by placing a disable command before the enable commands. Strange. No help with an additional enable command, wait 0 secs with messages, re-wording the enable commands with 'menu "Edit" of me', or replacing the disable/enable commands with "(".

Problem2: After the fix for problem1, if I use the Edit menu to cut and paste text, immediately select new text, I can't cut with ctrl-x. Using the menu doesn't send a mouseLeave message to re-enable menuItems for keyboard equivilents. No fix.

Any suggestions how to script a working standardized Edit menu? Terry

stack demo:
EnableTest.zip
(3.42 KiB) Downloaded 32 times

Re: Edit MenuItems Enable Problem

Posted: Thu Apr 17, 2014 10:07 pm
by jacque
Typically all the commands are done in the mouseDown handler. (I'm a little surprised to learn that mouseleave is supported in menus, but probably that's because I develop on a Mac where menus behave differently.)

What I always do is enable everything, and then disable the parts I don't want. In your example:

Code: Select all

on mouseDown
 repeat with x = 1 to 3
   enable menuitem x of btn "edit" of me
 end repeat
 switch (the short name of target())
   case "Edit"
     get selectedChunk()
       ... etc 

Re: Edit MenuItems Enable Problem

Posted: Sat Apr 19, 2014 8:35 pm
by TerryL
Thanks to Jacque's comments I removed the mouseLeave handler from the grp "Menubar 1" script and placed a commandKeyDown handler in the stack script to restore edit menu keyboard equivilents, which as I understand is cross-platform compatible. This fixed both problems.

I added Undo to the edit menu, and fixed a small problem with the menu bar width following the stack width in Geometry manager. I think everything works on Win 7, LC 6.5.2. A public stack with a pull-down menu is quite rare. I searched the forum and revOnline and found only two, both from Jacque. This one is free for anyone to use in their projects.

Could someone try it out on a Mac and verify? Terry

Code: Select all

--in grp "Menubar 1" script:
on mouseDown  --enable/disable menuItems
   switch (the short name of target())
      case "Edit"
         get selectedChunk()
         if it = empty then  --no inserted cursor, no selected text
            disable menuItem 3 of menu "Edit"  --cut
            disable menuItem 4 of menu "Edit"  --copy
            disable menuItem 5 of menu "Edit"  --paste
         else if word 2 of it > word 4 of it then  --inserted cursor
            disable menuItem 3 of menu "Edit"  --cut
            disable menuItem 4 of menu "Edit"  --copy
            enable menuItem 5 of menu "Edit"  --paste
         else  --selected text
            enable menuItem 3 of menu "Edit"  --cut
            enable menuItem 4 of menu "Edit"  --copy
            enable menuItem 5 of menu "Edit"  --paste
         end if
         break
   end switch
end mouseDown

--In stack script:
on commandKeyDown pKey  --cross-platform
   if pKey = "c" then copy
   else if pKey = "v" then paste
   else if pkey = "x" then cut
   else pass commandKeyDown
end commandKeyDown

Re: Pull-Down Menu Stack For NoSave Standalone

Posted: Thu Apr 24, 2014 7:54 pm
by TerryL
Added a button to assist changing the Help > About menuItem.

The pull-down menu is fully functional. Terry

Re: Edit MenuItems Enable Problem

Posted: Thu Apr 24, 2014 8:15 pm
by FourthWorld
TerryL wrote:Thanks to Jacque's comments I removed the mouseLeave handler from the grp "Menubar 1" script and placed a commandKeyDown handler in the stack script to restore edit menu keyboard equivilents, which as I understand is cross-platform compatible. This fixed both problems.
If memory serves, you shouldn't need to add a commandKeydown message as command keys which can trigger menu items cause a mouseDown to go to the menuGroup before the menus are rendered, thus allowing us to centralize menu updating code in one mouseDown handler in the menu group.

Re: Pull-Down Menu Stack For NoSave Standalone

Posted: Fri Apr 25, 2014 7:20 pm
by TerryL
I re-tested the disabled Edit menuItems at Richard's suggestion. The commandKeyDown handler in the stack script IS necessary. If the cut, copy, or paste menuItems are disabled, their keyboard equivalents are also disabled and remain disabled until the mouseDown handler in the menu bar group script re-enables them. For example, with the commandKeyDown handler commented out, cut and paste text with the Edit menu. Now text can't be cut or copied with the keyboard unless the commandKeyDown handler is restored.

The ctrl-A (select all) and ctrl-Z (undo) though were unnecessary and now removed from the commandKeyDown handler in the updated attachment. Thanks for looking over the code Richard. It really does help make it better. Terry