local c=createComboBox(MainForm.gbScanOptions)

c.Style='csDropDownList'
c.Items.add('All')
c.ItemIndex=0
c.Name='ScanOptionsModuleList'  


c.Align=alTop
c.BorderSpacing.Left=6
c.BorderSpacing.Right=6
c.BorderSpacing.Bottom=2

local modulelist

local lastprocessid

function FillList()
  --print("beep")
  local is64bit=targetIs64Bit()
  local op
  if is64bit then
    op='32'
  else
    op='64'
  end
  synchronize(function()  
    c.Items.beginUpdate()
    while c.Items.Count>1 do
      c.Items.delete(1)
    end
    c.Items.endUpdate()
  end)

  modulelist=enumModules()
  
  synchronize(function()
    if modulelist then
      local i
      for i=1, #modulelist do
        modulelist[i].OriginalName=modulelist[i].Name
        if modulelist[i].Is64Bit ~= is64bit then
          modulelist[i].OriginalName='_'..modulelist[i].OriginalName
          modulelist[i].Name=modulelist[i].Name..' ('..op..'-bit)'
        end

        c.Items.Add(modulelist[i].Name)
      end
      
      lastprocessid=getOpenedProcessID()
    end
  end)
end

c.OnMouseEnter=function(d)  
  if c.Items.Count<=1 then
    --print("enter")
    if FillListThread==nil then
      FillListThread=createThread(function(t)
        FillList()
        FillListThread=nil
      end)
      c.ItemIndex=0
    end
  end
end



c.OnDropDown=function(d)
  if (FillListThread==nil) and (lastprocessid~=getOpenedProcessID()) then
    FillListThread=createThread(function(t)
      FillList()
      FillListThread=nil
    end) 
  end
    
  while FillListThread do
    checkSynchronize(50)
  end
end

c.OnSelect=function(d)
  if c.ItemIndex>=1 then
    MainForm.FromAddress.Text=string.format("%.16x",modulelist[c.ItemIndex].Address)
    if modulelist[c.ItemIndex].Size==nil then
      modulelist[c.ItemIndex].Size=getModuleSize(modulelist[c.ItemIndex].OriginalName)
    end
    if modulelist[c.ItemIndex].Size then
      MainForm.ToAddress.Text=string.format("%.16x",modulelist[c.ItemIndex].Address+modulelist[c.ItemIndex].Size)
    else
      MainForm.ToAddress.Text="7fffffffffffffff"
    end
  else
    MainForm.FromAddress.Text="0000000000000000"
    MainForm.ToAddress.Text="7fffffffffffffff"
  end
end

local pmModuleListDropdown=createPopupMenu(MainForm)
pmModuleListDropdown.Name='pmModuleListDropdown'

miResetList=createMenuItem(MainForm)
miResetList.Name='miResetModuleListDropDown'
miResetList.Caption=translate('Reset')
miResetList.OnClick=function()
  lastprocessid=nil
  FillList()
    MainForm.FromAddress.Text="0000000000000000"
    MainForm.ToAddress.Text="7fffffffffffffff"  
end

pmModuleListDropdown.Items.add(miResetList)

c.PopupMenu=pmModuleListDropdown
c.Enabled=false
