menuItem

Martin Blackman martinblackman at gmail.com
Sat Apr 28 10:16:17 EDT 2007


Here's a couple of handlers that let you call them by the text of the
menuitem.  They may not be industrial strength but work OK for my
purposes

on EnableMenuItem tMenu tMenuItem
   --enable menu items and submenu items 1 deep (use | as submenu separator)
   if tMenuItem is empty then
      repeat with x = 1 to the number of lines in btn tMenu
         enable menuitem x of btn tMenu
      end repeat
   else
      --preprocess to remove shortcuts ,"&" and "(" data:
      put empty into tlines
      repeat for each line tlinetxt in btn tMenu
         put tlinetxt into tline  --avoid modifying repeat handle
         replace "&" with empty in tline
         replace "(" with empty in tline
         get offset ("/",tline)
         if it <> 0 then delete char it to -1 of tline
         put tline & return after tlines
      end repeat
      delete char -1 of tlines
      set the itemdel to "|"
      set the wholematches to true
      put lineoffset (item 1 of tMenuItem,tlines) into tlineNo
      enable menuitem tlineNo of btn tMenu
      --enable submenu item
      put item 2 of tMenuItem into tMenuItem
      add 1 to tlineNo
      put line tlineNo of btn tMenu into tline
      set the itemdel to tab
      repeat until item 2 of tline is empty
         if item 2 of tline = tMenuItem or tMenuItem is empty then
enable menuitem tlineNo of btn tMenu
         add 1 to tlineNo
         put line tlineNo of btn tMenu into tline
      end repeat
   end if
end EnableMenuItem

on disableMenuItem tMenu tMenuItem
    --disable menuitem and submenu items one deep (separated by |)
    if tMenuItem is empty then
        repeat with x = 1 to the number of lines in btn tMenu
            disable menuitem x of btn tMenu
        end repeat
    else
        --preprocess to remove shortcuts, "&" and "(" data:
        put empty into tlines
        repeat for each line tlinedata in btn tMenu
            put tlinedata into tline  --avoid modifying repeat variable
            get offset("/",tline)
            if it <> 0 then delete char it to -1 of tline  --remove
keyboard shortcut
            replace "&" with empty in tline
            replace "(" with empty in tline
            replace "/" with empty in tline
            put tline & return after tlines
        end repeat
        delete char -1 of tlines
        set the itemdel to "|"
        set the wholematches to true
        put lineoffset (item 1 of tMenuItem,tlines) into tMainlineNo
        put item 2 of tMenuItem into tMenuItem
        put 1 + tMainlineNo into tlineNo
        put line tlineNo of btn tMenu into tline
        set the itemdel to tab
        put true into DisableMainItem
        repeat until item 2 of tline is empty
            if item 2 of tline = tMenuItem or tMenuItem is empty then
disable menuitem tlineNo of btn tMenu else \
                    put false into DisablemainItem
            add 1 to tlineNo
            put line tlineNo of btn tMenu into tline
        end repeat
        if DisableMainItem then disable menuitem tMainlineNo of btn tMenu
    end if
end disableMenuItem

regards
Martin



More information about the use-livecode mailing list