-- OSCmenu_Handlers
-----------------------------------
-- PROPERTIES:
--!memberProperties: [#name: "OSCmenu_Handlers", #scripttype: #score, #scriptSyntax: #lingo, #comments: ""]
--
-- DESCRIPTION:
--              This script handles the parsing and handling of the menu with
--              all handlers of the current stage movie
--
-- REQUIRES:
--              script "aleXtrasMovieScript" as provider for library scripts -> xscr()
--              script "commonMovieScript" -- standard handlers
--
-- USAGE:
--              behavior which is either on the OSC control menu member
--                 (in case of using OSC for the menu display)
--              or an invisible shape mmember
--                 (in case of using buddyMenu to display the menu)
--              Accordingto the type of the sprite.member the corresponding xtra is used
-----------------------------------

property pCurrentHandlerList
property pCallerSprite
property pCallBackParams

on mShowOSCMenu me, callerSprite
  
  pCallerSprite = callerSprite
  
  kw = member("searchword").text
  
  hnd = mBuildList(kw)
  
  if count(hnd) < 1 then exit
  
  pCurrentHandlerList = hnd
  
  if sprite(me.spritenum).member.type = #OSmenu then
    
    li = mConvertListOSC(me, hnd)
    
    sprite(me.spritenum).itemList = li
    sprite(me.spritenum).click()
    
  else
    
    li = mConvertListBMenu(me, hnd)
    
    r = (the activewindow).rect
    p = sprite(me.spritenum).rect.offset(r.left, r.top)
    theResult = bmMenuAt(li, 16, p[1], p[4])
    
    if count(theResult) > 1 then mDoMenuChangeAction me, mGetListref(me, theResult, 1)
    
    if ilk(pCallerSprite) = #integer then
      sendSprite(pCallerSprite, #mUnhilite)
    end if
    
  end if
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on update me, theIndex, theName
  pCallBackParams = []
  pCallBackParams.add(theIndex)
  pCallBackParams.add(theName)
  
  --  put "------------------"
  --  put theIndex
  --  put theName
  --  put "------------------"
  
  xscr().mCreateTimeOut("osc_callback_to", 10, #mOSCCallback, me)
  -- break the event chain up, because when the event was generated in the xtra, we do not get error messages in lingo
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mOSCCallback me, dto, theIndex, theName
  
  if ilk(dto) = #timeout then dto.forget()
  
  if listP(pCallBackParams) then
    if count(pCallBackParams) > 0 then theIndex = pCallBackParams[1]
    if count(pCallBackParams) > 1 then theName = pCallBackParams[2]
  end if
  pCallBackParams = 0
  
  if listP(theIndex) then
    
    mDoMenuChangeAction me, mGetListref(me, theIndex, 0)
    
  end if
  
  if ilk(pCallerSprite) = #integer then
    sendSprite(pCallerSprite, #mUnhilite)
  end if
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mouseUp me
  if ilk(pCallerSprite) = #integer then
    sendSprite(pCallerSprite, #mUnhilite)
  end if
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-- convert the list to buddyMenu format

on mConvertListBMenu me, aList
  li = []
  cnt = count(aList)
  
  repeat with n = 1 to cnt
    
    if aList[n].getaprop(#type) = #divider then
      --      if count(li) > 0 then
      li.add("")
      --      end if
    else
      li.add(string(aList[n].getaprop(#title)))
    end if
    
    theItems = aList[n].getaprop(#items)
    if listP(theItems) then
      if count(theItems) > 0 then li.add(mConvertListBMenu(me, theItems))
    end if
    
  end repeat
  
  return li
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-- convert the list to OSControl format

on mConvertListOSC me, aList
  li = []
  cnt = count(aList)
  
  repeat with n = 1 to cnt
    
    sublist = []
    theItems = aList[n].getaprop(#items)
    if listP(theItems) then
      if count(theItems) > 0 then sublist = mConvertListOSC(me, theItems)
    end if
    
    if aList[n].getaprop(#type) = #divider then
      --      if count(li) > 0 then
      li.add([#text:"(-"])
      --      end if
    else
      if count(sublist) < 1 then
        li.add([#text:string(aList[n].getaprop(#title))])
      else
        li.add([#text:string(aList[n].getaprop(#title)), #submenu:sublist])
      end if
    end if
    
  end repeat
  
  return li
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mGetListref me, aList, forBuddyMenu
  listOfAll = pCurrentHandlerList
  cnt = count(aList)
  sprnum = 0
  repeat with n = 1 to cnt
    kw = aList[n]
    
    if forBuddyMenu = 1 then
      li = mGetItemByTitle(me, listOfAll, kw)
    else
      li = listOfAll[kw]
    end if
    
    if count(li) < 1 then exit repeat
    if li.getaprop(#type) = #divider then
      li = [:]
      exit repeat
    end if
    if sprnum > 0 then li.setaprop(#spritenum, sprnum)
    else if integerP(li.getaprop(#spritenum)) then sprnum = li.getaprop(#spritenum)
    listOfAll = li.getaprop(#items)
  end repeat
  if ilk(li) <> #proplist then li = [:]
  return li
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mGetItemByTitle me, li, kw
  repeat with n = count(li) down to 1
    if li[n].getaprop(#title) = kw then return li[n]
  end repeat
  return [:]
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mDoMenuChangeAction me, listref
  
  
  scrtype = listref.getaprop(#scripttype)
  if ilk(scrtype) = #symbol then
    if ((the optiondown + the commanddown + the controldown) > 0) and not(the shiftdown) then
      
      tell the stage
        if integerP(listref.getaprop(#memNum)) then
          if integerP(listref.getaprop(#clibNum)) then
            if listP(listref.getaprop(#sel)) then
              activateScriptEditor listref.getaprop(#memNum), listref.getaprop(#clibNum), listref.getaprop(#sel)[1], listref.getaprop(#sel)[2]
            end if
          end if
        end if
      end tell
      
    else
      
      cmdli = listref.getaprop(#title)
      cmd = cmdli.word[1]
      delete word 1 of cmdli
      
      glob = xscr().mGetGlobalList()
      
      glob[#spritenum] = ""
      
      if (scrtype = #movie) then
        
        
        glob[#cmd] = cmd
        glob[#theParams] = [:]
        glob[#theTargetInstance] = 0
        glob[#copy2ClipBoard] = the shiftdown + (the optiondown or the controldown)
        
        if cmdli.word.count then
          mShowMui cmdli
        else
          mDoCmd
        end if
        
      else if (scrtype = #score) then
        
        glob[#cmd] = cmd
        glob[#theParams] = [:]
        spnum = integer(listref.getaprop(#spritenum))
        if integerP(spnum) then glob[#spritenum] = spnum
        glob[#theTargetInstance] = 0
        glob[#copy2ClipBoard] = the shiftdown + (the optiondown or the controldown)
        glob[#isBehavior] = 1
        
        olddelim = the itemdelimiter
        the itemdelimiter = ","
        delete item 1 of cmdli
        the itemdelimiter = olddelim
        
        if length(cmdli) < 1 then cmdli = "spritenum"
        else put "spritenum, " before cmdli
        
        mShowMui cmdli
        
        
        --        if cmdli.word.count then
        --          mShowMui cmdli
        --        else
        --          mDoCmd
        --        end if
        
        --        if cmdli.word.count then
        --          put RETURN & "erg = sendSprite(x, #"&cmd&", "&cmdli&")" & RETURN
        --        else
        --          put RETURN & "erg = sendSprite(x, #"&cmd&")" & RETURN
        --        end if
        
        
      else if (scrtype = #parent) then
        
        olddelim = the itemdelimiter
        the itemdelimiter = ","
        delete item 1 of cmdli
        the itemdelimiter = olddelim
        
        glob[#cmd] = cmd
        glob[#theParams] = [:]
        
        glob[#copy2ClipBoard] = the shiftdown + (the optiondown or the controldown)
        
        tell the stage
          if integerP(listref.getaprop(#memNum)) then
            if integerP(listref.getaprop(#clibNum)) then
              glob[#theTargetInstance] = new(member(listref.getaprop(#memnum), listref.getaprop(#clibnum)).script)
            else
              glob[#theTargetInstance] = new(mGetXScript(symbol(listref.getaprop(#memName))))
              if glob.getaprop(#copy2ClipBoard) = 1 then glob.setaprop(#copy2ClipBoard, 2)
            end if
          else
            glob[#theTargetInstance] = new(mGetXScript(symbol(listref.getaprop(#memName))))
            if glob.getaprop(#copy2ClipBoard) = 1 then glob.setaprop(#copy2ClipBoard, 2)
          end if
        end tell
        
        glob[#theMemberName] = listref.getaprop(#memName)
        
        
        if cmdli.word.count then
          mShowMui cmdli
        else
          mDoCmd
        end if
        
      end if
      
    end if
    
  end if
  
end
